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