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.

example 1 · press Run, then edit and run again

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.

example 2 · press Run, then edit and run again

Hiding letters

example 3 · press Run, then edit and run again

Useful checks

💡 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:

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.

More Python concepts