str.strip() / str.lstrip() / str.rstrip()
| Since: | Python 2(2000) |
|---|
Methods that remove whitespace or specified characters from the beginning and/or end of a string. Commonly used for formatting user input and cleaning data.
Syntax
# Removes whitespace or specified characters from both ends of a string. str.strip(chars=None) # Removes only from the beginning (left end) of a string. str.lstrip(chars=None) # Removes only from the end (right end) of a string. str.rstrip(chars=None)
Method List
| Method | Description |
|---|---|
| str.strip(chars=None) | Removes any characters in chars from both ends of the string as long as they continue. If chars is omitted, whitespace characters (spaces, tabs, newlines, etc.) are removed. |
| str.lstrip(chars=None) | Removes specified characters from the beginning (left end) of the string. |
| str.rstrip(chars=None) | Removes specified characters from the end (right end) of the string. |
Sample Code
strip_basic.py
text = " Hello, Python! "
print(text.strip())
print(len(text))
print(len(text.strip()))
line = "\t\n data \n\t"
print(repr(line.strip()))
path = "///usr/local/bin"
print(path.lstrip("/"))
number_str = "000042"
print(number_str.lstrip("0"))
filename = "report.txt..."
print(filename.rstrip("."))
line_with_newline = "data row\n"
clean_line = line_with_newline.rstrip("\n")
print(repr(clean_line))
Running the code produces the following output:
python3 strip_basic.py Hello, Python! 20 14 'data' usr/local/bin 42 report.txt 'data row'
strip_chars.py
print("aaabbccXYZccbba".strip("abc"))
print("hello".strip("helo"))
Running the code produces the following output:
python3 strip_chars.py XYZ
strip_csv.py
csv_line = " Kiryu Kazuma , 27 , Former Yakuza "
fields = [field.strip() for field in csv_line.split(",")]
print(fields)
members = [" Nishikiyama Akira ", " Majima Goro ", " Sawamura Haruka ", " Kashiwagi Osamu "]
cleaned = [m.strip() for m in members]
print(cleaned)
url = "https://example.com"
domain = url.removeprefix("https://")
print(domain)
filename2 = "report.txt"
name = filename2.removesuffix(".txt")
print(name)
Running the code produces the following output:
python3 strip_csv.py ['Kiryu Kazuma', '27', 'Former Yakuza'] ['Nishikiyama Akira', 'Majima Goro', 'Sawamura Haruka', 'Kashiwagi Osamu'] example.com report
Common Mistakes
Common Mistake 1: strip() removes a set of characters, not a substring
The chars argument of strip() is treated as a set of characters to remove, not as a substring. For example, strip("Kiryu") removes any of the individual characters K, i, r, y, u from the ends — not the string "Kiryu" as a whole. Use removeprefix() or removesuffix() (Python 3.9+) to remove a prefix or suffix as a unit.
mistake1_ng.py
name = "Kiryu Kazuma"
result = name.strip("Kiryu")
print(result)
Running the code produces the following output:
python3 mistake1_ng.py Kazuma
Instead of removing "Kiryu" as a whole prefix, strip() removes the individual characters K, i, r, y, u from both ends. The space before "Kazuma" is not in the set, so removal stops there.
mistake1_ok.py
name = "Kiryu Kazuma"
result = name.removeprefix("Kiryu ")
print(result)
Running the code produces the following output:
python3 mistake1_ok.py Kazuma
Common Mistake 2: strip() cannot remove whitespace in the middle
strip() only removes characters from the start and end of a string. To remove extra whitespace in the middle, combine split() and join().
mistake2_ng.py
text = "Kiryu Kazuma is strong" print(text.strip())
Running the code produces the following output:
python3 mistake2_ng.py Kiryu Kazuma is strong
mistake2_ok.py
text = "Kiryu Kazuma is strong" normalized = " ".join(text.split()) print(normalized)
Running the code produces the following output:
python3 mistake2_ok.py Kiryu Kazuma is strong
Notes
The chars argument of strip() is treated as a set of characters to remove, not as a substring. For example, "abc".strip("ab") does not remove the sequence "ab" as a unit — it removes any a or b characters that appear at either end. To remove a prefix or suffix as a whole unit, use removeprefix() or removesuffix() (Python 3.9+).
All of these methods return a new string. The original string is not modified. When reading a file line by line, rstrip("\n") or rstrip() is commonly used to remove the trailing newline from each line.
For data cleaning that combines stripping with splitting, see also str.split(). For searching within strings, see str.find() / str.index().
If you find any errors or copyright issues, please contact us.