Python String Methods for Beginners, strip, split, join, replace
Most real data arrives as text: a name typed into a form, a line from a file, a filename, an email address. Before you can do anything useful with it, you usually need to clean it up, cut it into pieces, or check what is inside. Python strings come with built-in methods for all of that. A handful of them cover almost every day-to-day need. Every example was run in Python 3, and the output shown is what it printed.
1. First, the rule that explains everything
The official documentation describes strings as immutable: once created, a string cannot be changed. Every string method that seems to modify text actually returns a new string and leaves the original alone.
name = " Ada Lovelace "
name.strip()
print(repr(name))
clean = name.strip()
print(repr(clean))
Output:
' Ada Lovelace '
'Ada Lovelace'
The second line of code did the work but threw the result away. If you want to keep the cleaned version, assign it to a variable. Forgetting this is the most common reason a string method "doesn't work."
2. strip(): remove spaces from the ends
strip() removes whitespace from the start and end of a string, which is exactly what you want for values typed into forms. lstrip() and rstrip() do only the left or right side.
You can also pass characters to remove, but read the documentation's warning carefully: the argument is a set of characters, not a word to remove.
subject = "Re: Report"
print(subject.lstrip("Re: "))
print(subject.removeprefix("Re: "))
Output:
port
Report
lstrip("Re: ") kept removing any of the characters R, e, colon and space until it hit one that was not in the set, so it ate the "Re" from "Report" too. The documentation points to removeprefix() for removing one exact prefix, which is what was intended here. There is a matching removesuffix() for endings.
3. split(): cut a string into a list
split() returns a list of pieces. It behaves differently depending on whether you give it a separator:
print(" red green blue ".split())
print("red,,green".split(","))
print("2026-10-17 Team call: budget, hiring".split(" ", 1))
Output:
['red', 'green', 'blue']
['red', '', 'green']
['2026-10-17', 'Team call: budget, hiring']
With no argument, the documentation says runs of whitespace count as one separator and empty strings are dropped, so messy spacing disappears. With an explicit separator, two separators in a row produce an empty string between them. The second argument, maxsplit, limits how many cuts are made, which is handy for splitting a date off the front of a line.
4. join(): put a list back together
join() is the reverse of split(). The string you call it on becomes the glue between items:
colors = ["red", "green", "blue"]
print(", ".join(colors))
print(" / ".join(colors))
Output:
red, green, blue
red / green / blue
The documentation notes that join raises a TypeError if any item is not a string. Numbers trip people up:
ids = [104, 221, 309]
print(", ".join(ids))
TypeError: sequence item 0: expected str instance, int found
Convert each item first. map(str, ids) applies str() to every item:
print(", ".join(map(str, ids)))
104, 221, 309
5. replace(): swap one piece of text for another
phone = "555-010-4477"
print(phone.replace("-", ""))
print(phone.replace("-", ".", 1))
Output:
5550104477
555.010-4477
By default every match is replaced. The optional third argument limits how many.
6. lower(), upper(), and title(): fix capitalization
Comparisons in Python are exact, so "Ada@Example.COM" and "ada@example.com" are different strings. Lowercase both sides before comparing:
typed = "Ada@Example.COM"
saved = "ada@example.com"
print(typed == saved)
print(typed.lower() == saved)
False
True
title() capitalizes the first letter of each word, with a quirk the documentation calls out: apostrophes count as word boundaries.
print("o'neil's report".title())
O'Neil'S Report
Check the result before using title() on names.
7. find(), in, startswith(), endswith(): look inside
filename = "invoice_2026_final.pdf"
print(filename.find("2026"))
print(filename.find("2025"))
print("2025" in filename)
print(filename.endswith((".pdf", ".docx")))
Output:
8
-1
False
True
find() returns the position of the first match, counting from 0, or -1 if it is not found. The documentation recommends using in when you only need a yes or no answer. endswith() and startswith() accept a tuple, so one call can check several file types.
8. Putting it together
A line pasted from a spreadsheet, with stray spaces and mixed case:
line = " Ada Lovelace , ADA@Example.com , London "
name, email, city = line.split(",")
name = name.strip()
email = email.strip().lower()
city = city.strip()
print(" | ".join([name, email, city]))
Ada Lovelace | ada@example.com | London
split(",") cuts the line into three pieces, which are unpacked into three variables. Each piece is stripped, and the email is lowercased by chaining two methods. Chaining works because each method returns a new string that the next one can use.
9. Key takeaways
- Strings are immutable, so string methods return a new string; assign the result to keep it.
strip()removes characters from a set, not a word; useremoveprefix()to drop an exact prefix.split()with no argument handles messy whitespace; with a separator it keeps empty pieces.join()only accepts strings, so convert numbers withstr()first.- Use
infor a yes or no check, andfind()only when you need the position.