Python Strings Tutorial: Slicing, f-strings and String Methods with Examples
P3.3 Python String Methods: strip, lower, upper, replace, split
Why. Users type " mumbai", "MUMBAI " and "Mumbai". To a computer these are three different cities. Cleaning methods make them the same.
What. A method is a function attached to a value. You call it with a dot: text.lower().
| Method | What it does | Example | Result |
|---|---|---|---|
| strip() | removes spaces at both ends | " Pune ".strip() | "Pune" |
| lower() | all small letters | "PUNE".lower() | "pune" |
| upper() | all capital letters | "pune".upper() | "PUNE" |
| title() | first letter of each word capital | "new york".title() | "New York" |
| replace(a, b) | changes a into b | "1,200".replace(",", "") | "1200" |
| split(sep) | cuts into a list | "a,b".split(",") | ["a", "b"] |
How to read this table: each row is one method with a small input and the result it gives.
How.
Clean a messy city name
- Remove outer spaces with
strip(). - Fix the case with
title(). - Compare or store the clean value.
Worked example A — Clean spaces and case.
Step 1 — What we do. A form for a bank in Chennai collected the city with extra spaces and capital letters.
Step 2 — Type and run this code.
city = " CHENNAI "
clean = city.strip().title()
print(clean)
Step 3 — Output you should see.
Chennai
Step 4 — What it means. Methods can be chained: first strip(), then title(). Result: "Chennai".
Worked example B — Clean a money value.
Step 1 — What we do. An invoice from Johannesburg has the amount "R 12,450.00". We remove the symbol and comma.
Step 2 — Type and run this code.
raw = "R 12,450.00"
num = float(raw.replace("R", "").replace(",", "").strip())
print(num)
Step 3 — Output you should see.
12450.0
Step 4 — What it means. After cleaning, the text becomes the number 12450.0, ready for maths.
Worked example C — Split a full name and an email.
Step 1 — What we do. We split a full name into parts and take the domain from an email.
Step 2 — Type and run this code.
full = "Sofia Garcia Lopez"
parts = full.split(" ")
print(parts)
print(parts[0])
email = "sofia@example.com"
print(email.split("@")[1])
Step 3 — Output you should see.
['Sofia', 'Garcia', 'Lopez']
Sofia
example.com
Step 4 — What it means. split(" ") cuts at each space and gives a list. [0] takes the first item. The same idea finds email domains.
Worked example D — Join a list back into text.
Step 1 — What we do. We join a list of product tags into one line with commas.
Step 2 — Type and run this code.
tags = ["organic", "fresh", "local"]
print(", ".join(tags))
Step 3 — Output you should see.
organic, fresh, local
Step 4 — What it means. join is the opposite of split. The text before .join is put between items.
Ravindra Bagale's Tip
Strings never change in place. city.strip() gives a new string; write city = city.strip() to keep it.
Ravindra Bagale's Tip – मराठी
Strings जागेवर बदलत नाहीत. city.strip() नवी string देतो; ती ठेवायला city = city.strip() लिहा.
Ravindra Bagale's Tip – हिंदी
Strings अपनी जगह नहीं बदलते. city.strip() नई string देता है; उसे रखने के लिए city = city.strip() लिखो.