Skip to content
Hands-on Mentor
ENTR
Back to blog

Python string methods: the 10 you will use every day

Python string methods clean, split, search and change text. Learn strip, lower, split, join, replace and find, with real output from Corner Café orders.

4 min read

Python string methods are built-in actions you call on a piece of text, like "latte".upper(). They clean text (strip), change case (lower, upper), cut text into parts (split), join parts back (join), and search or replace (find, replace). A string never changes in place, so every method gives you a new string.

What is a string method in Python?

A method is a function that belongs to a value. You write the value, a decimal point, the method name and brackets.

name = "  Corner Café Latte  "
print(name.strip())
print(name.strip().lower())
print(name.strip().upper())
print(name.strip().title())
print(len(name), len(name.strip()))
Corner Café Latte
corner café latte
CORNER CAFÉ LATTE
Corner Café Latte
21 17

strip() removes spaces from both ends. You can chain methods: name.strip().lower() strips first, then lowers the result. len() is a function, not a method, but you use it with strings all the time. It shows that the 4 spaces are gone.

Do string methods change the original string?

No. Strings are immutable, which means they cannot be changed after they are created. A method returns a new string, and the old one stays the same.

s = "latte"
s.upper()
print(s)
s = s.upper()
print(s)
latte
LATTE

The first s.upper() makes "LATTE" and throws it away. To keep the result, assign it back to a variable. Forgetting this is the most common string mistake.

How do you split a string in Python?

split(separator) cuts a string into a list. The Corner Café menu file stores one item per line, with the name and price separated by a semicolon.

order = "latte;4.25"
parts = order.split(";")
print(parts)
item, price = parts
print(item, float(price))
print("2026-03-02".split("-"))
print(" Hello  world ".split())
['latte', '4.25']
latte 4.25
['2026', '03', '02']
['Hello', 'world']

Call split() with no argument and Python splits on any run of spaces and drops the empty parts. The price is still text after the split, so float(price) turns it into a number. To split only once, pass a second value: "a-b-c".split("-", 1) gives ['a', 'b-c'].

How do you join a list of strings?

join is the opposite of split. You write the separator first, then pass the list.

print(" | ".join(["latte", "tea", "brownie"]))
latte | tea | brownie

All items in the list must be strings. A list of numbers needs a conversion first, for example ", ".join(str(n) for n in numbers).

text = "Corner Café"
print(text.replace("Corner", "Harbor"))
print(text.startswith("Corner"), text.endswith("Bar"))
print(text.find("Café"), text.find("Bar"))
print("Café" in text)
print("latte latte tea".count("latte"))
Harbor Café
True False
7 -1
True
2
  • replace(old, new) swaps every match.
  • startswith and endswith answer yes or no.
  • find returns the position of the first match, or -1 if there is none. Positions start at 0.
  • in checks if a smaller string is inside a bigger one.
  • count counts non-overlapping matches.

A real cleaning example

Order files often have extra spaces and mixed case. Chain the methods you just learned.

raw = "  LATTE ;4.25 \n"
cleaned = raw.strip().lower().split(";")
print(cleaned)
['latte ', '4.25']

Look closely: there is still a space after latte. strip() only cleans the ends of the whole string, and the space sits in the middle, next to the semicolon. Clean each part after the split:

raw = "  LATTE ;4.25 \n"
item, price = [part.strip() for part in raw.lower().split(";")]
print(repr(item), repr(price))
'latte' '4.25'

Quick reference

MethodWhat it doesExample result
strip()Removes spaces at both ends" x " becomes "x"
lower() / upper()Changes case"Tea" becomes "tea"
title() / capitalize()Capitalizes words / first letter"latte" becomes "Latte"
split(sep)Text to list"a;b" becomes ["a", "b"]
join(list)List to text"-".join(["a", "b"]) becomes "a-b"
replace(a, b)Swaps text"tea".replace("t", "b") becomes "bea"
find(x)Position or -1"latte".find("t") becomes 2
count(x)Counts matches"latte".count("t") becomes 2
isdigit()Only digits?"425".isdigit() is True

Common mistakes

  • Not assigning the result. s.upper() alone does nothing useful.
  • Using a list method on a string. Strings have no append. This runs but fails:
"latte".append("x")

Python reports AttributeError: 'str' object has no attribute 'append'. Build a new string with + or an f-string instead.

  • Calling `isdigit()` on a price. "4.25".isdigit() is False because of the decimal point.
  • Forgetting that `find` returns -1. Do not use the result as an index without checking it.

What to read next

Practice these methods in the lesson [Text methods](/learn/python/text-methods), then see how to pull out parts of a string in [Python slicing explained](/blog/python-slicing). To put values into text, read [Python f-strings](/blog/python-f-strings). The whole [Python course](/courses/python) has 40 lessons and is free.

  • #python
  • #strings
  • #string methods
  • #text
All posts