String Methods and Formatting

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
python
msg = 'HELLO'
print(msg.lower())
Output
hello

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

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*

Edge Cases

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

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']

Edge Cases

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'))