Python string exercises
Strings are where most Python beginners meet their first real bug: an index one past the end, a method whose result was never kept, a number glued to text. Each exercise here is a short function that handles text and has exactly one thing wrong.
Read the symptom, find the line, fix it, and run the tests. They run in your browser with real Python, so there is nothing to install.
The strings bugs you will meet
- Indexing past the end: the last character is at len(text) - 1, not len(text).
- Strings cannot be changed in place, so replace(), strip() and lower() return a new string you have to keep.
- strip(".txt") removes characters, not a suffix; removesuffix() removes the exact ending.
- "Total: " + 5 is a TypeError; convert with str() or use an f-string.
- split(" ") and split() treat repeated spaces differently.
The exercises
Easy
- Formatter crashing on valid pricesType
- Missing config key crashes the lookupNull and undefined
- Text parser returning wrong countsLogic
- String casing remains lowercaseMutation and copying
- String checker throwing IndexErrorOff-by-one
- AI Attempt: The palindrome checkerType
- Name formatter crashes on the first letterType
- AI Attempt: FizzBuzz never says FizzBuzzLogic
- Birthday calculator crashes on the form inputType
- Vowel counter skips capital lettersLogic
- Price parser rejects prices with centsType
Errors you might be searching for
More Python practice by topic
Questions
Are these good for beginners?
Yes. They are sorted easy first, each is a few lines long, and the hints get warmer one at a time if you are stuck.
Where are the solutions?
Every exercise explains the bug once you have fixed it. Sign in and you can also compare your fix with the reference solution.