Python strip(), split(), join(), replace() for Beginners
strip() returns a copy of a string with whitespace removed from both ends; pass a string of characters and it strips any of those characters instead. It never changes the original string. 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.
On this page▾
- String methods leave the original unchanged
- strip(): remove spaces from the ends
- split(): cut a string into a list
- join(): put a list back together
- replace(): swap one piece of text for another
- lower(), upper(), and title(): fix capitalization
- find(), in, startswith(), endswith(): look inside
- Putting it together
- Frequently asked questions
- What does strip() do in Python?
- Does strip() change the original string?
- How do I remove only the left or right side?
- Does strip("abc") remove the text "abc"?
| Method | Main use |
|---|---|
strip() |
Remove end whitespace, or a specified set of characters |
split() |
Turn text into pieces |
join() |
Combine strings with a separator |
replace() |
Substitute matching text |
find() |
Return a position, or -1 when absent |
String methods leave the original unchanged
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."
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.
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.
Sorting the resulting lists is covered in Sorting in Python: key, reverse, and Sorting by More Than One Field.
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
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.
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.
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.
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.
Frequently asked questions
What does strip() do in Python?
It returns a copy of the string with the leading and trailing characters removed. If the characters argument is omitted or None, it removes whitespace, according to the Python documentation.
Does strip() change the original string?
No. Strings are immutable, so strip() returns a new string. Assign the result to a variable to keep it.
How do I remove only the left or right side?
Use lstrip() for the start of the string or rstrip() for the end.
Does strip("abc") remove the text "abc"?
No. The documentation says the argument is not a prefix or suffix; all combinations of its characters are stripped. To remove one exact prefix or suffix, use removeprefix() or removesuffix().
Sources
About this guide
Howlane operator · Checked against the official sources listed above. It is general information, not professional advice.
AboutEditorial standardsCorrectionsReport an error
Edit history
- Updated
- Updated
- Updated
- Updated
- Published