Python String strip() Method
By Lenin Mishra
If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.
Python String strip()
method
The strip()
method in Python,
returns a new string by removing(stripping)
all the characters from the left and right that are
passed as string argument. If no arguments are passed, it removes the leading and trailing whitespaces.
Syntax of strip()
method
string.strip()
Returns a new string
Example
Code/Output
x = " Pylenin loves Python "
print(x.strip())
>>> Pylenin loves Python
x = "*Pylenin loves Python*"
print(x.strip('*'))
>>> Pylenin loves Python
x = "*@Pylenin loves Python@*"
print(x.strip('*@'))
>>> Pylenin loves Python
Check out other commonly used Python string methods.
Related Articles
- How to create a string in Python?
- How to access characters in a Python string?
- How to replace characters in a string in Python?
- How to concatenate strings in Python?
- How to iterate through a string in Python?
- Check if a Substring is Present in a Given String in Python
- Escape sequences in Python String
- Python String Formatting - The Definitive Guide