Python 3.12 ยท
โ verified by execution on 2026-07-22
Python strings are full of handy tools called methods. Because strings are objects, they carry these built-in functions with them.
Case Conversion
You can easily change the casing of a string using methods like upper() and lower().
python
msg = 'Hello World'print(msg.upper())
Output
HELLO WORLD
Your output
python
msg = 'HELLO'print(msg.lower())
Output
hello
Your output
Immutability: Predict the Output!
Here is the most important rule about strings: Strings are immutable. That means they cannot be changed in place. When you run a method like upper(), it returns a brand new string.
python
msg = 'hello'msg.upper()print(msg)
Output
hello
Your output
Notice that printing msg still outputs hello in lowercase! If you want to keep the uppercase version, you must assign it to a variable: msg = msg.upper().
Cleaning Up Text
The strip() method is perfect for cleaning up messy user input. It removes leading and trailing characters (spaces by default).
python
msg = ' hello 'print(f'*{msg.strip()}*')
Output
*hello*
Your output
Edge Cases
strip() removes any combination of the specified characters, not just the exact sequence.
Replacing Substrings
To swap out parts of a string, use replace(). By default, it replaces all occurrences.
python
msg = 'I like apples'print(msg.replace('apples', 'bananas'))
Output
I like bananas
Your output
Splitting Text into Lists
The split() method breaks a single string into a list of smaller strings. By default, it splits on whitespace, but you can specify any delimiter.
python
csv = 'one,two,three'print(csv.split(','))
Output
['one', 'two', 'three']
Your output
Edge Cases
split() with no arguments correctly handles multiple consecutive spaces as a single separator, unlike split(' ').
Check yourself
Do string methods modify the original string in place?
Reveal answer
No, strings are immutable. Methods return a new string, leaving the original unchanged. โ Strings are immutable in Python. Any method that alters the text actually returns a brand new string.
What does the strip() method remove?
Reveal answer
Characters from the very beginning and very end (leading and trailing). โ strip() only affects the outside edges (leading and trailing), never the middle.
By default, how many occurrences does replace() swap?
Reveal answer
ALL occurrences unless a count argument is provided. โ replace() swaps every single match it finds by default.
What happens when you call split() without any arguments?
Reveal answer
It splits by any whitespace (spaces, tabs, newlines). โ By default, split() targets all whitespace, even if there are multiple spaces in a row.
Challenges
Challenge 1 +10 XP
You are given a messy name string. Convert it to title case and remove the extra spaces at the ends. Print the result.
python
Test 1 โ expects "John Doe\n"
Need a hint? (โ25% XP)
You can chain methods! Try strip() followed by title().
Show solution (0 XP)
name = ' jOhn dOe 'print(name.strip().title())
Challenge 2 +15 XP
Replace all occurrences of 'sad' with 'happy'. Print the result.
python
Test 1 โ expects "I am happy. Very happy.\n"
Need a hint? (โ25% XP)
Use the replace('old', 'new') method.
Show solution (0 XP)
mood = 'I am sad. Very sad.'print(mood.replace('sad', 'happy'))
Go deeper
In a Trie, Lookup Cost Depends on the Word, Not on How Many Words โ A trie stores words letter by letter, sharing every common prefix. Walk one here by clicking letters, and watch the reachable set shrink - that walk is exactly what an autocomplete box does while you type.
What Is a Token? Train a Tokenizer and Watch One Form โ Language models read tokens, not letters or words. Train a real byte-pair encoding tokenizer on your own text, watch the vocabulary build itself merge by merge, and see why models struggle to spell.
How Many R's in Strawberry? Why That Question Is Unfair โ A model that writes working code will confidently miscount the letters in a word. It is not a reasoning failure โ the letters were destroyed before the model saw anything. Train a tokenizer and watch it happen.