Variables & Data Lesson 9 of 23
Python String Methods
Clean and search text with upper, lower, title, strip, replace, find, count, startswith, split, join, isdigit and more.
Strings come with many built-in methods: actions you call with a dot, like name.upper(). They're essential for cleaning up what users type and for working with text files.
Changing case
name = "juan DELA cruz"
print(name.upper())
print(name.lower())
print(name.title())
print(name.capitalize())
Output
JUAN DELA CRUZ juan dela cruz Juan Dela Cruz Juan dela cruz
Methods return a new string; the original is unchanged. Save the result if you need it: name = name.title().
Removing spaces: strip()
typed = " ana@email.com "
print("[" + typed.strip() + "]")
print("[" + typed.lstrip() + "]")
print("[" + typed.rstrip() + "]")
Output
[ana@email.com] [ana@email.com ] [ ana@email.com]
Searching
text = "Ang pagbasa ay pagkatuto. Ang pagkatuto ay kapangyarihan."
print(text.count("Ang"))
print(text.find("pagkatuto")) # first position, or -1
print(text.startswith("Ang"))
print(text.endswith("."))
Output
2 15 True True
replace()
msg = "I love Math. Math is fun."
print(msg.replace("Math", "Python"))
print("09-17-123-4567".replace("-", ""))
Output
I love Python. Python is fun. 09171234567
split(): text to list
subjects = "Math,Science,English,Filipino"
print(subjects.split(","))
sentence = "Kaya natin ito"
words = sentence.split() # splits on spaces
print(words)
print(len(words), "words")
Output
['Math', 'Science', 'English', 'Filipino'] ['Kaya', 'natin', 'ito'] 3 words
join(): list to text
join is the opposite of split. Call it on the separator:
names = ["Ana", "Ben", "Carla"]
print(", ".join(names))
print(" → ".join(["Luzon", "Visayas", "Mindanao"]))
Output
Ana, Ben, Carla Luzon → Visayas → Mindanao
Checking what's inside
| Method | True when the string… |
|---|---|
isdigit() | contains only digits: "2026" |
isalpha() | contains only letters: "Rizal" |
isalnum() | letters and digits only: "Room101" |
isupper() / islower() | is all uppercase / lowercase |
isspace() | is only spaces |
age = input("Age: ").strip()
if age.isdigit():
print(f"In 10 years you'll be {int(age) + 10}.")
else:
print("Please type a whole number.")
Output
Age: fifteen Please type a whole number.
Chaining methods
answer = input("Continue? (yes/no) ").strip().lower()
if answer == "yes":
print("Okay, let's go!")
Output
Continue? (yes/no) YES Okay, let's go!
.strip().lower() handles spaces and capital letters in one line, so "YES", " yes" and "Yes" all work.
Padding and centering
print("7".zfill(3)) # pad with zeros
print("MENU".center(20, "="))
Output
007 ========MENU========
Example: initials and username
full_name = "Maria Clara Santos"
parts = full_name.split()
initials = "".join(p[0] for p in parts)
username = (parts[0] + parts[-1]).lower()
print("Initials:", initials)
print("Username:", username)
Output
Initials: MCS Username: mariasantos
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04