Day 4 · ~14m

Python Strings: Store and Display Text

A string is any text wrapped in quotes. Learn string properties, methods like strip(), upper(), and lower(), and how to clean whitespace from CSV data.

student (neutral)

So yesterday we wrote format_sale to join the name and amount together. But what exactly is a string — like, is it just any text in quotes?

teacher (curious)

Exactly. A string in Python is a sequence of characters wrapped in quotes — single or double, it doesn't matter which you choose, as long as you're consistent. "Alice Chen" and 'Alice Chen' are identical to Python.

Let's look at what happened yesterday:

def format_sale(name, amount):
    return name + ": $" + str(amount)

The name parameter is a string. The ": $" part is a string. We used str(amount) to turn the number into a string so we could join all three together. Python calls this string concatenation — linking text together with the + operator.

student (curious)

I noticed you said single and double quotes don't matter. Is that really true? I've definitely seen people use them differently.

teacher (amused)

This is famous. A lot of people think single quotes and double quotes work differently in Python. They don't — Python treats them identically. The confusion comes from other languages where they actually do behave differently. But in Python? Pure myth. Pick one and stick with it.

message1 = "Hello, Alice!"
message2 = 'Hello, Alice!'
print(message1)  # Same output
print(message2)  # Same output

Both return the exact same string. The only time you'd want to mix them is when one quote character appears inside the string:

saying = "It's going to rain"  # Double quotes on the outside
quote = 'He said "yes"'         # Single quotes on the outside

But otherwise? Don't overthink it.

student (confused)

OK but here's where I got stuck with my CSV data. When I export sales from our system, the customer names sometimes have extra spaces — like " Alice Chen " with spaces at the beginning and end. My format_sale function included those spaces in the output, which looks ugly.

teacher (encouraging)

This is a real problem you'll hit constantly with data. When you pull customer names from a CSV export, they come with accidental whitespace. Python has a method for exactly this: strip(). It removes spaces from both the beginning and end of a string.

name = "  Alice Chen  "
clean_name = name.strip()
print(clean_name)  # Output: "Alice Chen" (no leading or trailing spaces)

You're calling a method on the string — that's the dot notation. name.strip() means "use the strip method on this string." Python has dozens of string methods, but strip() is probably the one you'll use most often because real data is messy.

student (thinking)

OK, so every string has these methods? Can I see what else I can do?

teacher (focused)

You can transform text in all kinds of ways. Here are the ones that show up in almost every program:

name = "alice chen"

name.upper()      # Returns: "ALICE CHEN"
name.lower()      # Returns: "alice chen" (already lowercase, no change)
name.title()      # Returns: "Alice Chen" (first letter of each word capitalized)
name.strip()      # Removes spaces from both ends
len(name)         # Returns: 10 (the number of characters in the string)

Notice that len() is a bit different — it's not a method you call with a dot. It's a function. But the rest are methods: you call them on the string itself using dot notation.

student (excited)

So I could fix my CSV data by stripping spaces AND converting names to title case at the same time?

teacher (proud)

Exactly! You can chain them together:

messy_name = "  alice chen  "
clean_name = messy_name.strip().title()
print(clean_name)  # Output: "Alice Chen"

Python reads left to right: strip the spaces first, then apply title case to what's left. This is the kind of one-liner that saves you hours of manual cleanup.

student (surprised)

That's wild. So if I update format_sale to use .strip(), my output will be clean automatically?

teacher (serious)

Exactly. You'll call strip() on the name before using it in the function. This way, no matter what data gets passed in — " Alice Chen ", "Bob Kumar", " Carol Santos" — the output is always properly formatted. Right now you're building strings the old way with + signs and str(). There's a cleaner approach that professional Python developers use every day. Tomorrow I'll show you, and you'll never want to go back.