Start free

GCSE · 10 exercises

Python text processing

Go through text a character at a time, and build the answer as you go: a new string, or a list of pieces joined at the end with "".join().

ord() gives a character's number and chr() the character for a number: useful for shifting letters.

text = input()
out = []
for ch in text:
    out.append(ch.upper() if ch.islower() else ch.lower())
print("".join(out))

Watch for: Shifting letters past "z" must wrap round to "a": use % 26 on the letter's position in the alphabet.

Exercises

  1. Caesar cipherShift every letter forward in the alphabet by a number of places, wrapping from zIntermediate
  2. Run-length encodingCompress a word by writing each run of the same letter as the letter and how manyIntermediate
  3. Run-length decodingUndo run-length encoding: each letter is followed by how many times it repeats (theIntermediate
  4. camelCase to snake_caseConvert a camelCase name to snake_case: put an underscore before each capital letterIntermediate
  5. Roman numeralsConvert a Roman numeral to a number.Intermediate
  6. To binaryWrite a number in binary by repeatedly dividing by 2 and collecting the remainders.Intermediate
  7. No repeated lettersAn isogram is a word with no letter repeated.Intermediate
  8. Reverse each wordReverse the letters of every word but keep the words in their order.Intermediate
  9. Capitalise sentencesMake the first letter of every sentence a capital.Intermediate
  10. Simple email checkCheck an email address: exactly one @, something before it, and after it a dot withIntermediate
Start with “Caesar cipher”