Python String Exercises with Solution
Python String Exercises: String is a data type that is widely used in every programming language for performing the operation on textual data.
These programming exercises have been structured and designed in a beginner-friendly way, focusing on the core concepts of string data type operations.
The main idea behind solving these questions is to make your concept more clear and to improve logical thinking of how to approach a problem. We are going to cover string operations using some mix of conditional statements i.e if, if-else, for loop(), range(), while loop() etc.
For reference
Python String Exercises for beginners with Solution:
We will solve 10 python string exercises for beginners in python with a solution & detailed code explanation.
Exercise 1: Write a program to reverse a string in python.
Hint
Input: python
Expected output
Result is: nohtyp
Exercise 2: Write a program to count vowels and consonants in a string.
Hint
Input: python
Expected output
Vowels count is: 1
Consonant count is: 5
Exercise 3: Write a program to remove duplicates in a string.
Hint
Input: pythonlobby
Expected output
Result is: p y t h o n l b
Exercise 4: Write a program to count the number of letters in a word.
Hint
Input: pythonlobby
Expected output
Result is: 11
Exercise 5: Python program to count the occurrence of each character in a word.
Hint
Given x = programm
Expected output
Occurrence of each characters is :
{‘P’: 1, ‘r’: 2, ‘o’: 1, ‘g’: 1, ‘a’: 1, ‘m’: 2}
Exercise 6: Python program to convert lower letter to upper and upper letter to lower in a string.
Hint
Inut: PrOgRaMM
Expected output
Result is: pRoGrAmm
Exercise 7: Python program to search a specific word in a string.
Hint
Input:
Enter a String: I am a boy
Enter a word to search: boy
Expected output
boy exists in string
Hint
Input:
Enter a String: I am a boy
Enter a word to search: girl
Expected output
girl not exists in string
Exercise 8: Write a python program to sort letters of word by lower to upper case format.
Hint
Input:
Enter a String: pytHOnloBBy
Expected output
Result: p y t n l o y H O B B
Exercise 8: Write a program in Python to count lower, upper, numeric and special characters in a string.
Hint
Given x = @pyThOnlobb!Y34
Expected output
Numeric counts 2
Lower counts 8
Upper counts 3
Special counts 2
Exercise 9: Write a program in Python to remove an empty character from a list sequence.
Hint
Given n = [“name”,”age”,””,”hello”]
Expected output
Result: [‘name’, ‘age’, ‘hello’]
Exercise 10: Python program to convert all the starting letter of a word in upper case format or in the title format.
Hint
Given n = “hello this is pythonlobby”
Expected output
Result: Hello This Is Pythonlobby