
Python practice
Python string practice questions
15 problems, ordered from easiest. They run in your browser, and 8 are free.
Start with Bigrams from a sentenceThe idea in a minute
A string is text. Python can slice it, split it into pieces, change its case and search inside it, and most questions combine two or three of these.
The problems start with indexing and slicing and move to counting characters, checking palindromes and anagrams, and cleaning messy text.
Example: Count each letter
word = "banana"
counts = {}
for ch in word:
counts[ch] = counts.get(ch, 0) + 1
print(counts)
print(word[::-1], word.upper(), word.count("an"))What it prints
{'b': 1, 'a': 3, 'n': 2}
ananab BANANA 2What to watch for
- Strings cannot be changed in place. Every method returns a new string.
- Indexing starts at 0, and a slice stops before its end number.
- Use split() with no argument to split on any run of spaces.
Strings problems
- Bigrams from a sentencebeginner · Free
- Longest common prefixbeginner · Free
- Most common word in a support ticketbeginner · Free
- Reverse words in a log messagebeginner · Free
- Validate a discount code formatbeginner · Free
- Parse a delimited inventory linebeginner · Free
- Compact a run-length encoded event logbeginner · Free
- Expand a run-length encoded shift logbeginner · Free
- Top error codes from log linesintermediate · Locked
- Normalize a batch of phone numbersintermediate · Locked
- Match opening and closing tagsintermediate · Locked
- Parse a fixed-width mainframe extractintermediate · Locked
- Lazily read and filter a large log iteratorintermediate · Locked
- Safely parse a batch of numeric stringsintermediate · Locked
- Longest palindromic substringadvanced · Locked
Want a guided order across every topic? Open the practice sheets or the Python hub.