Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ The essentials
EXPERIMENT 04 / 101

Words with superpowers.

Slice, clean, and combine text without getting tangled in quotes.

BY NAVAN SAMI◷ 6 MIN READPYTHON 3

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.

strings.py
handle = "  navan  "
name = handle.strip().title()

print(f"Hello, {name}!")
print(name[:3])
print(len(name))
↳ EXPECTED OUTPUT
Hello, Navan!
Nav
5

03 Under the hood

  1. 1

    strip() returns a string without leading or trailing whitespace. title() returns a title-cased version. The original handle is unchanged.

  2. 2

    name[:3] selects positions 0, 1, and 2. The end position of a slice is excluded.

  3. 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

A SMALL MISSION

Clean the text " mission control ", make it uppercase, and print its first seven characters.

Reveal one possible solution +
one_possible_solution.py
label = "  mission control  ".strip().upper()
print(label[:7])

05 Check your signal

QUICK SIGNAL CHECK

What does "Orbit"[:2] return?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

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 ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.