Words with superpowers.
Slice, clean, and combine text without getting tangled in quotes.
01 The idea
A string is an immutable sequence of Unicode characters. You can access characters by position, slice out a section, or call methods that produce new strings. An f-string lets you insert values into text by writing an expression inside braces.
02 The experiment
Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.
handle = " navan "
name = handle.strip().title()
print(f"Hello, {name}!")
print(name[:3])
print(len(name))Hello, Navan! Nav 5
03 Under the hood
- 1
strip() returns a string without leading or trailing whitespace. title() returns a title-cased version. The original handle is unchanged.
- 2
name[:3] selects positions 0, 1, and 2. The end position of a slice is excluded.
- 3
len(name) counts the characters in this example. Use f-strings to build messages instead of manually converting every inserted number to text.
04 Your turn, builder
Clean the text " mission control ", make it uppercase, and print its first seven characters.
Reveal one possible solution +
label = " mission control ".strip().upper()
print(label[:7])05 Check your signal
What does "Orbit"[:2] return?
Experiment. Logged.
When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.
Keep exploring
Go deeper with the official Python guide ↗.