String Exercises

These exercises are designed to deeply test your understanding of Python strings. They focus on logic, slicing, methods, formatting, and edge cases.

Exercise 1: Reverse Words, Not Sentence

Problem: Reverse each word in a string, but keep the word order the same.
Input
python
text = "Python is very powerful"
Expected Output
text
nohtyP si yrev lufrewop

Exercise 2: Count Vowels and Consonants

Problem: Count the number of vowels and consonants in a string.
Input
python
text = "Programming"
Expected Output
text
Vowels: 3
Consonants: 8

Exercise 3: Check Palindrome (Ignore Case & Spaces)

Problem: Check whether a string is a palindrome. Ignore spaces and case sensitivity.
Input
python
text = "Never odd or even"
Expected Output
text
Palindrome

Exercise 4: Remove Duplicate Characters

Problem: Remove duplicate characters from a string while keeping the first occurrence.
Input
python
text = "programming"
Expected Output
text
progamin

Exercise 5: Longest Word in a Sentence

Problem: Find the longest word in a sentence.
Input
python
text = "Python makes programming enjoyable"
Expected Output
text
programming

Exercise 6: Character Frequency Dictionary

Problem: Create a dictionary that stores the frequency of each character (ignore spaces).
Input
python
text = "hello world"
Expected Output
python
{'h':1, 'e':1, 'l':3, 'o':2, 'w':1, 'r':1, 'd':1}

Exercise 7: Mask Sensitive Information

Problem: Mask all characters of a string except the last 4 characters.
Input
python
card_number = "1234567812345678"
Expected Output
text
************5678

Exercise 8: Capitalize Every Alternate Word

Problem: Capitalize every alternate word in a sentence.
Input
python
text = "python string exercises are challenging"
Expected Output
text
python STRING exercises ARE challenging

Exercise 9: Extract Domain from Email

Problem: Extract the domain name from an email address.
Input
python
email = "student@codemarathi.com"
Expected Output
text
codemarathi.com

Exercise 10: Check Anagram Strings

Problem: Check if two strings are anagrams (ignore spaces and case).
Input
python
s1 = "Listen"
s2 = "Silent"
Expected Output
text
Anagram

Exercise 11: Compress the String

Problem: Compress the string by counting consecutive characters.
Input
python
text = "aaabbccccd"
Expected Output
text
a3b2c4d1

Exercise 12: Find First Non-Repeating Character

Problem: Find the first non-repeating character in a string.
Input
python
text = "swiss"
Expected Output
text
w

Exercise 13: Validate Password Strength

Problem: Check if a password:
  • Has at least 8 characters
  • Contains uppercase, lowercase, digit, and special character
Input
python
password = "Py@12345"
Expected Output
text
Strong Password

Exercise 14: Remove Special Characters

Problem: Remove all special characters from a string.
Input
python
text = "Py@th#on!123"
Expected Output
text
Python123

Exercise 15: Rotate String

Problem: Rotate a string to the left by n characters.
Input
python
text = "Python"
n = 2
Expected Output
text
thonPy

Final Challenge 🔥

Problem: Given a sentence, return the word with:
  1. Highest vowel count
  2. If tie, return the first one
Input
python
text = "education is very important"
Expected Output
text
education

Note for Learners

Hard-Level Insight

If you can solve these exercises without looking at solutions, your string concepts are interview-ready.