▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
Working with text (strings)
Join, slice and format text with f-strings.
🎬 Video version coming soon — read it now
Text in Python is called a string (str): characters inside quotes.
Joining strings
You can't add a string and a number directly. "Age: " + 21 gives a TypeError. Turn the number into text first with str(21), or use an f-string.
f-strings: the best way to mix text and values
Put an f before the quote and wrap variables in { }:
:.1f means "show one decimal place". You'll use f-strings in almost every program you write.
Length, indexing and slicing
Handy string methods
Methods return a new string. The original doesn't change, because strings can't be changed in place. To keep the result, store it: text = text.strip().
help(str.split) in Colab to read about any method.In one line
Strings hold text; join them with +, build them with f-strings, and slice them with [start:end].
Share with a friend