Python Strings for Games
Python strings explained with word games: indexing letters, slicing, upper and lower, split and join, checking letters with in, and formatting with f-strings.
Strings are text. Word games like Hangman and Word Guess are really string games: you look at letters, hide them, compare them and join them back together.
Split and join
.split() breaks text into a list of words (great for commands like take key). " ".join(list) glues a list back into text.
Hiding letters
Useful checks
.isdigit(): only digits? (safe toint()).isalpha(): only letters?.startswith()/.endswith().replace(old, new)and.count(x)
💡 Strings can't be changed in place: word[0] = "P" fails. Build a new string instead, for example "P" + word[1:].
Games that use Strings
Practise this with 7 of the 20 games:
- #02☆☆☆
Rock Paper Scissors in Python
Learn The random module, Dictionaries and if, elif and else.
- #04☆☆☆
Hangman in Python
Learn Strings, Lists and while loops.
- #07☆☆☆
Mad Libs-Style Story Game in Python
Learn input() and print(), Strings and Variables.
- #08☆☆☆
Text Adventure Game in Python
Learn Dictionaries, Strings and while loops.
- #12☆☆☆
Battleship-Style Game in Python
Learn 2D lists and grids, The random module and Strings.
- #15☆☆☆
Word Guessing Game in Python
Learn Strings, Lists and Dictionaries.
- #20☆☆☆
Typing Speed Game in Python
Learn Strings, Functions and for loops.
Frequently asked questions
How do I loop through the letters of a string?
for letter in word: visits each character in order. Add enumerate() to get the position too.
How do I reverse a string in Python?
word[::-1] returns the string backwards.