Python String strip() Function: How to use?
⚡ Smart Summary
Python strip() removes leading and trailing characters from a string and returns a clean copy. By default it deletes whitespace, while an optional character argument removes any of the specified characters from both ends of the original string.

What is Python strip()?
Python strip() is one of the built-in functions available in the Python library. The strip() method removes the given characters from the start and end of the original string. By default, strip() removes white spaces from the start and end of the string and returns the same string without the surrounding white spaces.
Syntax of the strip() method
string.strip([characters])
Parameters
- characters: (optional) The given characters will be removed from the start and end of the original string.
- If the characters parameter is not given, the white spaces from the start and end of the string will be removed.
Return Value
The Python String strip() method will return:
- The original string with white spaces removed from the start and end if the characters to be removed are not specified.
- In case the string does not have any white spaces at the start or end, the string will be returned as it is and will match the original string.
- If the characters parameter is given and the characters match, the characters at the start or end of the string will be removed, and the rest of the string will be returned.
- In case the characters given do not match the start or the end of the original string, the string will be returned as it is.
Examples of strip() Function in Python
The following examples show how the strip() method behaves with default whitespace, with an invalid data type, and with custom character parameters.
Example 1: strip() Method in Python
str1 = "Welcome to Guru99!" after_strip = str1.strip()
Output:
Welcome to Guru99!
Example 2: strip() on Invalid Data Type
The Python String strip() function works only on strings and will return an error if used on any other data type, such as a list or tuple.
Example when used on a list():
mylist = ["a", "b", "c", "d"] print(mylist.strip())
The above will throw an error:
Traceback (most recent call last):
File "teststrip.py", line 2, in <module>
print(mylist.strip())
AttributeError: 'list' object has no attribute 'strip'
Example 3: strip() Without character parameter
str1 = "Welcome to Guru99!" after_strip = str1.strip() print(after_strip) str2 = "Welcome to Guru99!" after_strip1 = str2.strip() print(after_strip1)
Output:
Welcome to Guru99! Welcome to Guru99!
Example 4: strip() Passing character parameters
str1 = "****Welcome to Guru99!****"
after_strip = str1.strip("*")
print(after_strip)
str2 = "Welcome to Guru99!"
after_strip1 = str2.strip("99!")
print(after_strip1)
str3 = "Welcome to Guru99!"
after_strip3 = str3.strip("to")
print(after_strip3)
Output:
Welcome to Guru99! Welcome to Guru Welcome to Guru99!
Python lstrip() and rstrip() Methods
The strip() method trims both ends of a string at once, but Python also provides two one-sided variants. The lstrip() method removes characters only from the left, or start, of the string, while the rstrip() method removes characters only from the right, or end. Like strip(), both remove whitespace by default and accept an optional set of characters to strip.
str1 = "***Guru99***"
print(str1.lstrip("*"))
print(str1.rstrip("*"))
Output:
Guru99*** ***Guru99
The three methods differ only in which side they trim. Applied to the padded string ” Guru99 “, the results are:
- strip() removes whitespace from both ends, returning “Guru99”.
- lstrip() removes whitespace from the left only, returning “Guru99 “.
- rstrip() removes whitespace from the right only, returning ” Guru99″.
Why Python strip() function is used?
Here are the main reasons for using the Python strip() function:
- It helps to remove the characters at the start and the end of the string based on the characters given to be removed from the original string.
- If the characters given do not match the original string, the string will be returned as it is.
- If the characters to be removed are not specified, the white spaces from the start and end of the original string will be removed.
- If there is no white space at the start or end, the string will be returned as it is.
» Learn more about Python String methods
