Day 11 · ~14m

Strings Deep Dive

String methods, slicing, case conversion, and split/join patterns.

🧑‍💻

OK so last week we used f-strings to format output, and I used .upper() in the quiz by accident. But I want to actually understand string methods. Like, how many of these things are there?

👩‍🏫

There are dozens, but you'll use about ten of them regularly. They're string methods — built-in functions that every string comes with. You call them with dot notation. And here's the key thing: they don't change the original string. Strings are immutable in Python — once created, they can't be modified. Instead, each method returns a new string.

name = "alice"
print(name.upper())    # ALICE
print(name.lower())    # alice
print(name.title())    # Alice
print(name)            # alice — original unchanged

.upper() converts every character to uppercase. .lower() does the opposite. .title() capitalizes the first letter of each word — handy for formatting names from messy data.

🧑‍💻

That's interesting. In my spreadsheets I use UPPER() and PROPER() all the time for cleaning up name columns. This is basically the same thing?

👩‍🏫

Exactly the same idea. .title() is Python's version of PROPER(). And just like in your spreadsheet, the original cell doesn't change — you get a new value. Python works the same way with strings.

🧑‍💻

What about getting just part of a string? Like, I often need to grab the first few characters of an account code.

👩‍🏫

That's called slicing. You use square brackets with a start and stop index, separated by a colon:

word = "Python"
print(word[0:3])   # Pyt — characters at index 0, 1, 2
print(word[2:5])   # tho — characters at index 2, 3, 4
print(word[:4])    # Pyth — from the start to index 3
print(word[3:])    # hon — from index 3 to the end
print(word[-3:])   # hon — last 3 characters

The stop index is exclusiveword[0:3] gives you indexes 0, 1, and 2 but not 3. Leaving out the start means "from the beginning." Leaving out the stop means "to the end." Negative indexes count from the end.

🧑‍💻

OK, and .split()? I keep seeing it everywhere. Remember when we looped through lists last week? Is .split() how you turn a string into a list?

👩‍🏫

Exactly. .split() breaks a string into a list of pieces. By default it splits on whitespace:

sentence = "Python is fun"
words = sentence.split()
print(words)  # ['Python', 'is', 'fun']

You can split on any character. This is huge for processing data — think CSV files, log lines, anything with a separator:

data = "Alice,92,A"
parts = data.split(",")
print(parts)  # ['Alice', '92', 'A']

.join() is the reverse — it takes a list and glues the pieces together with a separator:

words = ['Python', 'is', 'fun']
result = " ".join(words)
print(result)  # Python is fun

result2 = " - ".join(words)
print(result2)  # Python - is - fun

Notice the syntax: the separator string calls .join(), not the list.

🧑‍💻

Are there methods to check what a string contains? Like, in my spreadsheets I use SEARCH() and FIND() to look for specific text.

👩‍🏫

Yes. .startswith() and .endswith() return booleans. .count() tells you how many times a substring appears. .replace() swaps one substring for another:

email = "alice@example.com"
print(email.endswith(".com"))     # True
print(email.startswith("bob"))    # False
print(email.count("e"))           # 2

fixed = email.replace("example", "zuzu")
print(fixed)  # alice@zuzu.com

.strip() removes leading and trailing whitespace — essential when processing user input or file data:

raw = "   hello   "
print(raw.strip())   # hello
🧑‍💻

Can I chain methods together? Like, strip the whitespace AND title-case it in one go?

👩‍🏫

Absolutely. Since each method returns a new string, you can call another method on the result:

name = "  alice smith  "
clean = name.strip().title()
print(clean)  # Alice Smith

Read left to right: strip the whitespace, then title-case the result. This is exactly like chaining functions in a spreadsheet formula, but way more readable. Now let's put these methods to work.

Practice your skills

Sign up to write and run code in this lesson.

Already have an account? Sign in