Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. str.strip() / str.lstrip() / str.rstrip()

str.strip() / str.lstrip() / str.rstrip()

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

MethodDescription
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

# Use strip() to remove leading and trailing whitespace.
text = "   Hello, Python!   "
print(text.strip())   # 'Hello, Python!'
print(len(text))       # 20 (original length)
print(len(text.strip()))  # 14 (length after stripping)

# Tabs and newlines are also removed.
line = "\t\n  data  \n\t"
print(repr(line.strip()))  # 'data'

# A typical use case: sanitizing user input.
# user_input = input("Enter your name: ")
# name = user_input.strip()  # Remove surrounding whitespace before use.

# Use lstrip() to remove characters only from the beginning.
path = "///usr/local/bin"
print(path.lstrip("/"))  # usr/local/bin

number_str = "000042"
print(number_str.lstrip("0"))  # 42

# Use rstrip() to remove characters only from the end.
filename = "report.txt..."
print(filename.rstrip("."))  # report.txt

# Remove trailing newlines when reading lines from a file.
line_with_newline = "data row\n"
clean_line = line_with_newline.rstrip("\n")
print(repr(clean_line))  # 'data row'

# Note on the chars argument: it is treated as a set of characters, not a substring.
# Passing "abc" removes any combination of a, b, and c from the ends.
print("aaabbccXYZccbba".strip("abc"))  # XYZ
print("hello".strip("helo"))           # (results in an empty string)

# Practical example: strip extra spaces from CSV fields.
csv_line = "  Alice  ,  20  ,  Tokyo  "
fields = [field.strip() for field in csv_line.split(",")]
print(fields)  # ['Alice', '20', 'Tokyo']

# removeprefix() and removesuffix() (Python 3.9+) are also useful.
url = "https://example.com"
domain = url.removeprefix("https://")
print(domain)  # example.com

filename2 = "report.txt"
name = filename2.removesuffix(".txt")
print(name)  # report

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 .