Camp 2 · Step 7 of 23
Strings: working with text
Combine text, measure it, transform it — and meet f-strings, the tool you'll use in every program from now on.
Text values are called strings (a "string of characters"). You've been
using them since your first print — now let's actually work with them.
Gluing strings together
The + operator concatenates (joins) strings:
But mixing text and numbers with + fails — Python refuses to guess:
Run it: TypeError. You could convert with str(age)… but there's a much
better way.
f-strings: the good way
Put an f before the opening quote, and anything inside {curly braces}
gets evaluated and inserted:
f-strings handle numbers, do math right inside the braces, and read almost like the final sentence. From this lesson on, they're our default tool.
Useful string tools
len(...)— how many characters (spaces count!).upper()/.lower()— SHOUTING / whispering.title()— Capitalize Each Word.replace(old, new)— swap text
What does this print?
n = 3
print(f"{n} + {n} = {n + n}")What is len("hi there")?
What's next
Numbers ✓ and text ✓. Next comes the third fundamental type — booleans — the True/False values that let programs make decisions.