String Exercises

3 min read ·

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
Expected Output

Exercise 2: Count Vowels and Consonants

Problem: Count the number of vowels and consonants in a string.
Input
Expected Output

Exercise 3: Check Palindrome (Ignore Case & Spaces)

Problem: Check whether a string is a palindrome. Ignore spaces and case sensitivity.
Input
Expected Output

Exercise 4: Remove Duplicate Characters

Problem: Remove duplicate characters from a string while keeping the first occurrence.
Input
Expected Output

Exercise 5: Longest Word in a Sentence

Problem: Find the longest word in a sentence.
Input
Expected Output

Exercise 6: Character Frequency Dictionary

Problem: Create a dictionary that stores the frequency of each character (ignore spaces).
Input
Expected Output

Exercise 7: Mask Sensitive Information

Problem: Mask all characters of a string except the last 4 characters.
Input
Expected Output

Exercise 8: Capitalize Every Alternate Word

Problem: Capitalize every alternate word in a sentence.
Input
Expected Output

Exercise 9: Extract Domain from Email

Problem: Extract the domain name from an email address.
Input
Expected Output

Exercise 10: Check Anagram Strings

Problem: Check if two strings are anagrams (ignore spaces and case).
Input
Expected Output

Exercise 11: Compress the String

Problem: Compress the string by counting consecutive characters.
Input
Expected Output

Exercise 12: Find First Non-Repeating Character

Problem: Find the first non-repeating character in a string.
Input
Expected Output

Exercise 13: Validate Password Strength

Problem: Check if a password:
  • Has at least 8 characters
  • Contains uppercase, lowercase, digit, and special character
Input
Expected Output

Exercise 14: Remove Special Characters

Problem: Remove all special characters from a string.
Input
Expected Output

Exercise 15: Rotate String

Problem: Rotate a string to the left by n characters.
Input
Expected Output

Final Challenge

Problem: Given a sentence, return the word with:
  1. Highest vowel count
  2. If tie, return the first one
Input
Expected Output

Note for Learners

Hard-Level Insight

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

Learn Python String Exercises | Python Course