In Python, string exercises help you improve your understanding of strings by practicing real problems. Strings are one of the most important data types in Python, and mastering them is essential for programming, problem-solving, and interviews.
This guide contains basic to intermediate string exercises with solutions to help you practice step by step.
🔹 Why Practice String Exercises?
✔ Improve problem-solving skills
✔ Understand string methods deeply
✔ Prepare for coding interviews
✔ Build logic for real applications
✔ Strengthen Python fundamentals
🔹 Exercise 1: Print a String
🔹 Question:
Print "Hello Python".
print("Hello Python")
🔸 Output:
Hello Python
🔹 Exercise 2: Find String Length
🔹 Question:
Find the length of a string.
text = "Python"
print(len(text))
🔸 Output:
6
🔹 Exercise 3: Convert to Uppercase
🔹 Question:
Convert a string to uppercase.
text = "python"
print(text.upper())
🔸 Output:
PYTHON
🔹 Exercise 4: Convert to Lowercase
text = "PYTHON"
print(text.lower())
🔸 Output:
python
🔹 Exercise 5: Reverse a String
🔹 Question:
Reverse a string using slicing.
text = "Python"
print(text[::-1])
🔸 Output:
nohtyP
🔹 Exercise 6: Count Characters
🔹 Question:
Count how many times a character appears.
text = "banana"
print(text.count("a"))
🔸 Output:
3
🔹 Exercise 7: Check Substring
🔹 Question:
Check if "Python" exists in a string.
text = "I love Python programming"
if "Python" in text:
print("Found")
🔸 Output:
Found
🔹 Exercise 8: Replace Word in String
text = "I love Java"
new_text = text.replace("Java", "Python")
print(new_text)
🔸 Output:
I love Python
🔹 Exercise 9: Remove Spaces
text = " Python "
print(text.strip())
🔸 Output:
Python
🔹 Exercise 10: Split String into List
text = "apple,banana,mango"
print(text.split(","))
🔸 Output:
['apple', 'banana', 'mango']
🔹 Exercise 11: Join List into String
words = ["Python", "is", "fun"]
print(" ".join(words))
🔸 Output:
Python is fun
🔹 Exercise 12: Check Palindrome
🔹 Question:
Check if a string is a palindrome.
text = "madam"
if text == text[::-1]:
print("Palindrome")
else:
print("Not Palindrome")
🔸 Output:
Palindrome
🔹 Exercise 13: Count Vowels in String
text = "python programming"
vowels = "aeiou"
count = 0
for char in text:
if char in vowels:
count += 1
print(count)
🔹 Exercise 14: First Character Uppercase
text = "python programming"
print(text.title())
🔸 Output:
Python Programming
🔹 Exercise 15: Remove Special Characters
text = "P-y-t-h-o-n"
print(text.replace("-", ""))
🔸 Output:
Python
🔹 Exercise 16: Extract First Word
text = "Python is easy"
print(text.split()[0])
🔸 Output:
Python
🔹 Exercise 17: Check String Type
text = "Python123"
print(text.isalnum())
🔸 Output:
True
🔹 Exercise 18: Find Position of Word
text = "Hello Python"
print(text.find("Python"))
🔸 Output:
6
🔹 Exercise 19: Format String Output
name = "Alex"
age = 22
print(f"{name} is {age} years old")
🔸 Output:
Alex is 22 years old
🔹 Exercise 20: Word Frequency
text = "python python java python"
print(text.count("python"))
🔸 Output:
3
🔹 Real-Life Practice Example: Username Cleaner
username = " Alex123 "
clean = username.strip().lower()
print(clean)
🔸 Output:
alex123
🔹 Real-Life Practice Example: Email Validator
email = "user@gmail.com"
if email.endswith("@gmail.com"):
print("Valid Gmail")
🔹 Important Concepts Covered
✔ String indexing
✔ String slicing
✔ String methods
✔ String manipulation
✔ Condition checking
✔ Loops with strings
🔹 Tips for Solving String Problems
✔ Understand problem requirements
✔ Break string into small parts
✔ Use slicing when needed
✔ Practice built-in methods
✔ Test with different inputs
🚀 Conclusion
Python string exercises are essential for mastering text-based problem solving. Regular practice helps you build strong logic and prepares you for coding interviews and real-world programming.
Once you master these exercises, you can easily:
- Solve interview problems
- Build applications
- Handle text data efficiently
- Improve coding speed and accuracy


0 Comments