In Part 1, you learnt the basics of strings: quotes, printing, joining, length, and indexes.
In Part 2, you discovered escaping characters, multi-line strings, and slicing.
Now, in Part 3, we’ll explore string methods — special built-in tools that come with Python. These methods let you quickly change, tidy, or check strings without writing lots of extra code yourself. Think of them like “apps” that come pre-installed for working with text.
Changing the case of text
Strings often need to be written in a certain style — all capitals, all lowercase, or with proper names capitalised. Python makes this easy.
name = "alice"
print(name.upper()) # 'ALICE' (all caps)
print(name.lower()) # 'alice' (all lowercase)
print(name.title()) # 'Alice' (capitalises each word)
print(name.capitalize()) # 'Alice' (only first word capitalised)
These methods don’t change the original string — they create a new version.
Try this
- Create a variable with your name in lowercase and print it in uppercase.
- Try
.title()with"the lord of the rings". What happens?
Removing extra spaces
Sometimes when text comes from user input or files, it contains extra spaces at the beginning or end. This can cause errors or messy output. Python has methods to clean this up:
text = " hello "
print(text.strip()) # 'hello' (spaces removed from both sides)
print(text.lstrip()) # 'hello ' (spaces removed from the left)
print(text.rstrip()) # ' hello' (spaces removed from the right)
This is especially useful when you’re asking someone to type something with input() — they may accidentally press spacebar before or after.
Try this
- Make
word = " Python ". - Print it normally, then with
.strip(). Can you see the difference?
Replacing parts of a string
If you want to swap one piece of text for another, use .replace(old, new). This is useful for editing sentences or fixing mistakes.
sentence = "I like cats"
print(sentence.replace("cats", "dogs")) # 'I like dogs'
You can even chain replacements to make several changes at once.
Try this
- Make
phrase = "I love chips". - Replace
"chips"with"pizza". - Replace
"love"with"adore".
Splitting and joining
Strings often need to be broken into pieces (like splitting a sentence into words), or joined back together.
.split(separator)breaks a string into a list."separator".join(list)joins a list back into a single string.
items = "apple,banana,cherry"
print(items.split(",")) # ['apple', 'banana', 'cherry']
words = ["red", "green", "blue"]
print("-".join(words)) # 'red-green-blue'
This is useful when handling CSV files (comma-separated values) or when formatting data neatly.
Try this
- Split
"one two three"into a list of words. - Join
["tea", "coffee", "juice"]into one string separated by;.
Checking what’s inside a string
Python can test if a string contains certain types of characters. This is helpful when validating input, such as checking if someone typed a number or a word.
print("123".isdigit()) # True (all digits)
print("hello".isalpha()) # True (letters only)
print("hi123".isalnum()) # True (letters and numbers)
print(" ".isspace()) # True (only spaces)
You can also check how a string begins or ends:
filename = "report.pdf"
print(filename.endswith(".pdf")) # True
print(filename.startswith("rep")) # True
Try this
- Make
code = "PY123". Use.isalnum()to check it. - Make
filename = "photo.jpg". Check if it ends with.jpg.
Putting it all together
Here’s a short program that uses several methods together:
sentence = " learning python is fun "
# Step 1: tidy spaces
tidy = sentence.strip()
# Step 2: capitalise nicely
tidy = tidy.title()
# Step 3: replace words
tidy = tidy.replace("Fun", "Awesome")
print(tidy) # 'Learning Python Is Awesome'
This shows how you can combine methods step by step to clean up and improve text.
Try this
Write a program that:
- Asks the user for their favourite food (use
input()). - Removes spaces from the answer with
.strip(). - Prints it back in uppercase.
Summary
.upper(),.lower(),.title(),.capitalize()change the case of text..strip(),.lstrip(),.rstrip()remove spaces..replace()swaps text..split()breaks a string into a list,.join()joins a list into a string..isdigit(),.isalpha(),.isalnum(),.isspace()check the content of strings..startswith()and.endswith()check beginnings and endings.
✅ With these methods, you can now clean up, transform, and check text with just a few simple commands.










