String Manipulation Using Python String Functions
Write a Python program that takes a sentence from the user and performs the following
operations using the provided string functions. Follow the syntax given for each task.
Tasks:
1. Find the length of the string
Use the len() function to find and print the length of the string.
Syntax: len(string)
2. Convert the string to uppercase
Use the upper() method to convert the string to uppercase and print the result.
Syntax: string.upper()
3. Convert the string to lowercase
Use the lower() method to convert the string to lowercase and print the result.
Syntax: string.lower()
4. Count the occurrences of a specific character
Use the count() method to count how many times a specific character (given by the
user) occurs in the string and print the result.
Syntax: string.count(substring)
5. Check if the string starts with a specific prefix
Use the startswith() method to check if the string starts with a prefix (given by the
user) and print True or False.
Syntax: string.startswith(prefix)
6. Check if the string ends with a specific suffix
Use the endswith() method to check if the string ends with a suffix (given by the user)
and print True or False.
Syntax: string.endswith(suffix)
7. Find the first occurrence of a substring
Use the find() method to find the first occurrence of a substring (given by the user)
and print the index.
Syntax: string.find(substring)
8. Replace a word in the string
Use the replace() method to replace a specific word or substring (given by the user)
with another word and print the new string.
Syntax: string.replace(old, new)
9. Remove leading and trailing spaces
Use the strip() method to remove leading and trailing spaces from the string and print
the result.
Syntax: string.strip()
10. Split the string into words
Use the split() method to split the string into a list of words and print the list.
Syntax: string.split()
11. Join a list of words back into a string
Use the join() method to join the list of words (with a delimiter of your choice) and
print the result.
Syntax: delimiter.join(list_of_words)
Password Validator
Write a Python program using a function validate_password(password) to check if a password
meets the following criteria:
At least 8 characters long
Contains both uppercase and lowercase characters
Contains at least one digit
Contains at least one special character (e.g., @, #, $, etc.)
Example:
Input: password = 'Abc@1234'
Output: Valid password
Input: password = 'abc123'
Output: Invalid password: Does not meet criteria
Banking System Simulation
Write a Python program simulating a simple banking system where users can perform
operations like deposit, withdraw, and check balance. Implement functions for each of these
operations.
deposit(balance, amount) – Adds the amount to the balance.
withdraw(balance, amount) – Deducts the amount from the balance if sufficient funds are
available.
check_balance(balance) – Displays the current balance.
Reverse a String
Write a function reverse_string(s) that takes a string as input and returns the string reversed.
Example Input: "hello"
Example Output: "olleh"
Check for Palindrome
Write a function is_palindrome(s) that checks if a given string is a palindrome (reads the same
backward as forward). The function should ignore case.
Example Input: "Madam"
Example Output: True
Count Vowels
Write a function count_vowels(s) that counts the number of vowels (a, e, i, o, u) in a given string
and returns the count.
Example Input: "Hello World"
Example Output: 3
Extract Initials from a Name
Write a function get_initials(name) that takes a person's full name as input and returns their
initials.
Example Input: "John Doe"
Example Output: "J.D."