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
- Word count says 3 for "hello world"Logic
- Shout function returns lowercase textMutation and copying
- Last letter crashes with IndexErrorOff-by-one
- AI Attempt: The palindrome checkerType
- Capitalizing a name crashes with TypeErrorType
- AI Attempt: FizzBuzz never says FizzBuzzLogic
- Age next year crashes on form inputType
- Vowel counter skips capital lettersLogic
- Price parser rejects prices with centsType
- Letter counter crashes on the first letterNull and undefined
- Quiz marks "Paris " wrong because of a spaceLogic
- Name splitter crashes on a middle nameMutation and copying
- Setting "False" still turns the switch onType
- Word search misses a match at the startLogic
- Timer shows 2:5 instead of 2:05Mutation and copying
- 3 tickets at 20 gives "333…" instead of 60Type
- Preview cuts off one letter too soonOff-by-one
- Reversing words also reverses the lettersLogic
- Shopping list numbering starts at 0Off-by-one
- Tags after a comma start with a spaceMutation and copying
- PIN builder crashes on a list of digitsType
- Prices show $2.5 instead of $2.50Mutation and copying
- Joining path with leading slash discards base directoryLogic
- Matching digits inside string returns None with re matchLogic
- Stripping file suffix strips filename characters as a setLogic
- Counting words using space delimiter overcounts on multiple spacesLogic
- Stripping protocol prefix strips domain characters as setLogic
- Four digit year string raises ValueError with lowercase y formatType
- Sum function with string elements raises TypeErrorType
- String split on newline retains empty trailing element unlike splitlinesLogic
- Single quoted dictionary string raises JSONDecodeErrorType
- Longest-word finder returns the shortestLogic
- Reversed text shows <reversed object at 0x…>Type
- Word filter finds "cat" inside "cats"Logic
- Ingredient list ends with a stray commaOff-by-one
- File type check reads .tar.gz as tarLogic
- Phone number cleaner changes nothingLogic
- Newsletter sign-up ignores every yesLogic
- Long-word counter returns 0 for elephantLogic
- Initials come out as the second lettersOff-by-one
- Lucky number check never finds 5Type
- Tag list ends with an empty tagMutation and copying
- Name formatter turns McDonald into McdonaldLogic
- Greeting text crashes with TypeError on ageType
- First three characters slice drops the third letterOff-by-one
Medium
- Last 3 letters comes back with 4Off-by-one
- Removing double spaces freezesInfinite loops
- Highest score picks "9" over "10"Type
- Comparing form values to a number crashesType
- Spam filter removes only the first "spam"Logic
- Adding up "4,5,6" crashesType
- Admin check rejects the adminLogic
- AI Attempt: The anagram checkerLogic
- Label maker says str is not callableScope and closure
- Removing https:// eats the start of shop.comMutation and copying
- AI Attempt: The bracket checkerLogic
- Headline formatter writes Don'TMutation and copying
- AI Attempt: Caesar cipher runs off the alphabetLogic
- Number check rejects negative numbersType
- Empty basket reports one itemLogic
- Removing .txt turns test.txt into esLogic
- Express-only shipping comes out as single lettersType
- AI Attempt: First non-repeating characterLogic
- AI Attempt: Longest common prefixOff-by-one
- AI Attempt: Reversing a negative number crashesType
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.