What is a method?
A method is a function that belongs to a value. You use it with a dot: text.upper(). String methods give you a new string and leave the original unchanged.
Changing case
text = "Hello World"
print(text.upper())
print(text.lower())
print(text.title())
print(text.swapcase())Output
HELLO WORLD hello world Hello World hELLO wORLD
Removing extra spaces
strip() removes spaces from both ends. Great for cleaning up what users type:
messy = " hello "
print("[" + messy + "]")
print("[" + messy.strip() + "]")Output
[ hello ] [hello]
Replacing text
sentence = "I like cats. Cats are cute."
print(sentence.replace("cats", "dogs"))
print(sentence.replace("Cats", "Dogs"))Output
I like dogs. Cats are cute. I like cats. Dogs are cute.
Splitting into words
split() breaks a string into a list of pieces. You'll learn about lists in module 7.
sentence = "Python is really fun"
words = sentence.split()
print(words)
print(len(words), "words")
date = "16-10-2026"
print(date.split("-"))Output
['Python', 'is', 'really', 'fun'] 4 words ['16', '10', '2026']
Searching
email = "asha@example.com"
print("@" in email)
print(email.startswith("asha"))
print(email.endswith(".com"))
print(email.find("@"))
print(email.count("a"))Output
True True True 4 3
| Method | What it does |
|---|---|
upper() / lower() | ALL CAPS / all small letters |
strip() | remove spaces at both ends |
replace(a, b) | swap every a for b |
split() | break into a list of words |
find(x) | index of x, or -1 if not found |
count(x) | how many times x appears |
startswith(x) / endswith(x) | True or False |
Common mistake: Forgetting the brackets:
text.upperwithout()doesn't call the method.