Zum Inhalt springen
KodeTrail

Camp 2 · Schritt 7 von 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.

14 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

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:

Python

But mixing text and numbers with + fails — Python refuses to guess:

Python

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:

Python

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

Python
  • len(...) — how many characters (spaces count!)
  • .upper() / .lower() — SHOUTING / whispering
  • .title() — Capitalize Each Word
  • .replace(old, new) — swap text
Checkpoint

What does this print?

n = 3
print(f"{n} + {n} = {n + n}")
Checkpoint

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.