Python String Coding Questions
1. The Spy’s Hidden Message
A secret agent from the Intelligence Bureau receives encrypted messages that are
written backward to hide their content.
To read them, you need to reverse the string and return the decoded message.
Example Test Cases:
Input: "nohtyP" → Output: "Python"
Input: "egassem" → Output: "message"
Input: "IA" → Output: "AI"
Input: "gnimmargorP" → Output: "Programming"
Input: "esrever" → Output: "reverse"
2. Song Analyzer – Count the Melody
A music app wants to analyze lyrics to measure how "melodic" they are by counting
vowels (a, e, i, o, u).
Your job is to return how many vowels appear in the given lyric line.
Example Test Cases:
Input: "Hello World" → Output: 3
Input: "Education Rocks" → Output: 6
Input: "Sky" → Output: 0
Input: "AI Tools" → Output: 4
Input: "Beautiful Day" → Output: 6
3. The Mirror Username
An online game wants usernames that read the same both ways for a “Mirror
Challenge”.
You must check if a given username is a palindrome. Return True or False.
Example Test Cases:
Input: "madam" → Output: True
Input: "level" → Output: True
Input: "python" → Output: False
Input: "noon" → Output: True
Input: "data" → Output: False
4. Password Inspector
A cybersecurity app validates passwords.
The rule: Passwords must contain at least one uppercase and one lowercase letter.
Print "Strong" if it meets the criteria, otherwise "Weak".
Example Test Cases:
Input: "Python3" → Output: Strong
Input: "HELLO" → Output: Weak
Input: "world" → Output: Weak
Input: "PyThOn" → Output: Strong
Input: "code123" → Output: Weak
5. Clean My Notes
A note-taking app wants to remove all spaces from user notes to save memory.
Write a program that removes all spaces from the given sentence.
Example Test Cases:
Input: "Python is fun" → Output: "Pythonisfun"
Input: " Hello " → Output: "Hello"
Input: "a b c" → Output: "abc"
Input: "Open AI" → Output: "OpenAI"
Input: " No Space " → Output: "NoSpace"
6. Character Frequency in Chat
A social app analyzes the frequency of each character in a message to detect spam
patterns.
Return a dictionary showing each character and its count.
Example Test Cases:
Input: "banana" → Output: {'b':1, 'a':3, 'n':2}
Input: "aabbcc" → Output: {'a':2, 'b':2, 'c':2}
Input: "hello" → Output: {'h':1, 'e':1, 'l':2, 'o':1}
Input: "Python" → Output: {'P':1, 'y':1, 't':1, 'h':1, 'o':1,
'n':1}
Input: "aaaa" → Output: {'a':4}
7. Extract the Email Domain
A college automation system needs to extract the domain name (after '@') from each
student’s email ID.
Example Test Cases:
Input: "user@[Link]" → Output: "[Link]"
Input: "student@[Link]" → Output: "[Link]"
Input: "info@[Link]" → Output: "[Link]"
Input: "abc@[Link]" → Output: "[Link]"
Input: "team@[Link]" → Output: "[Link]"
8. Message Flipper
A chat app creates “mock” versions of messages by swapping case (uppercase →
lowercase, lowercase → uppercase).
Example Test Cases:
Input: "Python" → Output: "pYTHON"
Input: "HELLO" → Output: "hello"
Input: "hello123" → Output: "HELLO123"
Input: "PyThOn" → Output: "pYtHoN"
Input: "Data" → Output: "dATA"
9. Auto Word Replacement
An AI writing tool replaces certain keywords with better suggestions.
Write a program that replaces a given word with a new one in a sentence.
Example Test Cases:
Input: "I like Java", "Java", "Python" → Output: "I like Python"
Input: "Hello world", "world", "AI" → Output: "Hello AI"
Input: "Data Science", "Data", "ML" → Output: "ML Science"
Input: "Old car", "car", "bike" → Output: "Old bike"
Input: "Smart watch", "watch", "phone" → Output: "Smart phone"
10. Word Twins (Anagram Check)
A puzzle app asks players to check if two given words contain the same characters in
different orders — i.e., anagrams.
Example Test Cases:
Input: "listen", "silent" → Output: True
Input: "earth", "heart" → Output: True
Input: "python", "typhon" → Output: True
Input: "hello", "world" → Output: False
Input: "data", "tada" → Output: True
11. Headline Word Counter
A blogging platform shows how many words are in an article headline.
Example Test Cases:
Input: "Python is fun" → Output: 3
Input: "AI is the future" → Output: 4
Input: "Hello" → Output: 1
Input: "ChatGPT from OpenAI" → Output: 3
Input: "Data Science Rocks" → Output: 3
12. Longest Word Finder
A dictionary app highlights the longest word in a sentence to suggest vocabulary
improvements.
Example Test Cases:
Input: "Python is amazing" → Output: "amazing"
Input: "AI is fast" → Output: "fast"
Input: "OpenAI creates ChatGPT" → Output: "ChatGPT"
Input: "Data Science" → Output: "Science"
Input: "I love coding" → Output: "coding"
13. Text Cleaner – Remove Punctuation
A text cleaning app removes punctuation marks before performing NLP analysis.
Example Test Cases:
Input: "Hello, world!" → Output: "Hello world"
Input: "
[email protected]" → Output: "Python310"
Input: "It's fun!" → Output: "Its fun"
Input: "Hi...there??" → Output: "Hithere"
Input: "Good-day!" → Output: "Goodday"
14. Count Numbers in Username
An online portal ensures that usernames don’t contain too many digits.
Write a program to count digits in a given string.
Example Test Cases:
Input: "abc123" → Output: 3
Input: "hello" → Output: 0
Input: "2025year" → Output: 4
Input: "AI12345" → Output: 5
Input: "x9y8z7" → Output: 3
15. Unique Character Detector
A detective agency encrypts names by selecting the first non-repeating character from
each.
Find the first character that doesn’t repeat.
Example Test Cases:
Input: "aabbccde" → Output: "d"
Input: "racecars" → Output: "e"
Input: "python" → Output: "p"
Input: "aabb" → Output: None
Input: "swiss" → Output: "w"
16. Remove Repeated Letters
A data-cleaning system removes duplicate letters from text but keeps the first
occurrence.
Example Test Cases:
Input: "programming" → Output: "progamin"
Input: "aabbcc" → Output: "abc"
Input: "hello" → Output: "helo"
Input: "banana" → Output: "ban"
Input: "mississippi" → Output: "misp"
17. Title Formatter
A book publisher’s automation tool converts book titles so each word starts with a
capital letter.
Example Test Cases:
Input: "python is fun" → Output: "Python Is Fun"
Input: "hello world" → Output: "Hello World"
Input: "data science" → Output: "Data Science"
Input: "learn python" → Output: "Learn Python"
Input: "chatgpt rocks" → Output: "Chatgpt Rocks"
18. Login Checker (Ignore Case)
A login system verifies usernames without case sensitivity (e.g., "Admin" = "admin").
Example Test Cases:
Input: "Admin", "admin" → Output: True
Input: "Python", "PYTHON" → Output: True
Input: "User", "user1" → Output: False
Input: "AI", "ai" → Output: True
Input: "Hello", "HELLo" → Output: True
19. Word Game – Common Letters
You are creating a word game that rewards players for finding common letters between
two words.
Example Test Cases:
Input: "python", "typhoon" → Output: {'p', 'h', 'o', 'n', 't',
'y'}
Input: "apple", "grape" → Output: {'a', 'p', 'e'}
Input: "hello", "world" → Output: {'l', 'o'}
Input: "abc", "xyz" → Output: set()
Input: "data", "tada" → Output: {'a', 'd', 't'}
20. Hide Sensitive Numbers
A privacy filter replaces all digits in a message with ‘*’ to hide sensitive data.
Example Test Cases:
Input: "My pin is 1234" → Output: "My pin is ****"
Input: "Pass2025" → Output: "Pass****"
Input: "abc123xyz" → Output: "abc***xyz"
Input: "NoDigits" → Output: "NoDigits"
Input: "9Lives" → Output: "*Lives"