Strings

Working with text

Beginner 11 min

In this lesson

Strings are how Python handles text, and you’ll use them constantly. You’ll learn to build them, pick them apart by position, and transform them with built-in methods.

Explain it like I’m 5

A string is a row of characters in a fixed order — like beads on a thread. You can read any bead by its position and make new threads, but you can’t swap a bead in place.

Quotes, escaping, and multi-line text

Use single or double quotes — pick one and be consistent. To include the same quote inside, either use the other kind ("it's fine") or escape it with a backslash ('it\'s fine'). \n means a new line. Triple quotes ("""...""") let a string span several lines.

Example
print('it\'s fine')
print("line 1\nline 2")
Output
it's fine
line 1
line 2
Escaping a quote, and \n for a new line.

Joining text: + and f-strings

You can join strings with + ("Py" + "thon""Python"), but the cleaner way to mix text and values is an f-string: put f before the quote and drop variables into braces — f"Hi, {name}!". f-strings get their own lesson later; they’re worth getting comfortable with.

Example
greeting = "Py" + "thon"
name = "Ada"
print(greeting)
print(f"Hi, {name}! Welcome to {greeting}.")
Output
Python
Hi, Ada! Welcome to Python.
Joining with + versus dropping values into an f-string.

Indexing and slicing

Characters have positions starting at 0. word[0] is the first character; word[-1] is the last. A slice grabs a range: word[0:3] takes positions 0, 1, 2 (the end is excluded). Slicing always makes a new string.

Example
word = "Python"
print(word[0], word[-1])
print(word[0:3])
print(len(word))
Output
P n
Pyt
6
Indexing, slicing, and length.

Methods, and immutability

Strings come with handy methods: .upper(), .lower(), .strip() (remove surrounding spaces), .replace(a, b), and .split(). These all return a new string — the original never changes, because strings are immutable. So you must capture the result: name = name.strip().

Example
name = "  Ada Lovelace  "
print(name.strip())
print(name.strip().upper())
print(name.strip().split())
Output
Ada Lovelace
ADA LOVELACE
['Ada', 'Lovelace']
Chaining string methods.

Take the messy name below, strip the spaces, and print it in uppercase. Bonus: print just the first character.

name = "   grace hopper   "
print(name.strip().title())

More string methods you’ll reach for

A handful of extra methods come up again and again. .startswith() and .endswith() test the ends of a string; .find() returns the position of some text (or -1 if it’s missing); .count() tallies how many times something appears; and .replace(old, new) swaps text.

Two more are a bridge to lists: .split() breaks a string into a list of pieces, and .join() glues a list back into a string. (When you’re building text from variables, an f-string is usually cleaner than +.)

Example
email = "[email protected]"
print(email.endswith(".com"))
print(email.split("@"))

words = ["learn", "python", "today"]
print("-".join(words))
Output
True
['ada', 'example.com']
learn-python-today
Testing, splitting, and joining text.

Common mistake: Calling a string method but ignoring the result

Why it happens:

Because strings are immutable, methods return a new string instead of changing the original. Writing name.upper() on its own line throws the new value away.

How to fix it:

Capture the result: name = name.upper() (or use it directly in a print).

name = "ada"
name = name.upper()
print(name)

What does "hello"[1] return?

After s = "hi"; s.upper(), what is s?

Mini exercise (medium)

Given email = " [email protected] ", produce a clean lowercase address with no surrounding spaces and print it.

Do it here. Finish the code below and press Run to try your answer right in the browser — no setup needed.

email = " [email protected] "
clean = email      # TODO: strip the spaces and lowercase it
print(clean)

What to learn next

You learned to work with text: quoting and escaping, multi-line strings, indexing and slicing, the handy string methods, and why strings are immutable. You put it to work by cleaning up a messy email with .strip() and .lower().

With text covered, the other half of everyday data is numbers. Numbers explains int versus float, the arithmetic operators, true versus floor division, modulo, and the floating-point surprise that catches everyone once. When you’re ready, continue to the next lesson.