Start free

GCSE · 22 exercises

Python strings

A string is text. len() counts its characters; word[0] is the first, word[-1] the last; word[1:4] is a slice, from position 1 up to (not including) 4.

Methods give a new string and leave the old one as it was: .upper(), .lower(), .strip(), .replace(old, new), .split().

word = input()
print(len(word))
print(word.upper())
print(word[::-1])  # backwards

Watch for: word.upper() on its own line changes nothing: keep the result (word = word.upper()) or print it.

Exercises

  1. How long is it?Read a line of text and print how many characters it has (spaces count).Novice
  2. Make it loudRead a line and shout it: print it in capital letters followed by three exclamationNovice
  3. WhisperRead a line and print it in small letters.Novice
  4. First and last letterRead a word and print its first and its last character, separated by a space.Novice
  5. Repeat a wordRead a word and a number n, and print the word n times without spaces.Novice
  6. Spaces to underscoresRead a line and replace every space with an underscore, like in file names.Novice
  7. Count the letter aRead a line and print how many times the small letter a appears in it.Novice
  8. Title caseRead a line and print it with the first letter of every word in capitals.Novice
  9. Trim the spacesRead a line that may have spaces at the start and the end.Novice
  10. First three lettersRead a word and print its first three characters.Novice
  11. BackwardsRead a word and print it backwards.Novice
  12. Count the vowelsCount the vowels (a, e, i, o, u, small or capital) in a line.Beginner
  13. PalindromeA palindrome reads the same backwards, like level.Beginner
  14. Count the wordsCount the words in a line.Beginner
  15. Longest wordPrint the longest word in a line.Beginner
  16. AcronymMake an acronym from the first letter of every word, in capitals.Beginner
  17. Spell it outPrint the letters of a word separated by dashes.Beginner
  18. Remove the vowelsPrint a line without its vowels (a, e, i, o, u, small or capital).Beginner
  19. Digits in textCount the digits (0-9) in a line of text.Beginner
  20. How many times?Count how often a letter appears in a line, ignoring capital letters.Beginner
  21. Strong passwordA password is strong when it has at least 8 characters, at least one digit and atBeginner
  22. SpOnGeBoB casePrint a word with its letters alternating: the 1st, 3rd, 5th... in small letters andBeginner
Start with “How long is it?”