In this article, you will learn about the Python string strip()
method along with some examples.
The python string strip()
method is the best string method to remove leading and trailing characters from the string. strip function in python is a string built-in function.
To understand this method you should have basic knowledge of Python string.
In the previous tutorial, we have been seen the Python string lower()
method to convert uppercase characters to lowercase characters.
Headings of Contents
Python String strip() Method
Python strip() string method removes all the leading ( spaces at the beginning ) and trailing ( spaces at the end ) characters from the string. It takes a char type parameter which is optional.
if the parameter is not provided then it removes all the spaces from the beginning and end of the string.
Syntax
The syntax of the Python string strip() method is:-
string.strip(characters)
Parameter
string strip()
function in Python supports one argument that is characters.
- characters – ( Optional ) A set of characters to remove as leading and trailing characters.
Python string strip example
Here we will take some examples to understand strip function in Python.
Example 1:
a = ' PROGRAMMING '
result = a.strip()
print(result)
Output will be:- PROGRAMMING
Example 2:
a = '$$Programming$$$'
result = a.strip('$')
print(result)
The output will be:- PROGRAMMING
Conclusion:
In this python string method tutorial, you have learned the Python string strip method to remove all the leading and trailing characters from the string.
If you want to remove all the spaces from the beginning and end of the string, Then you can go with the strip function.
I hope this article will have helped you. if you like this article, please share it with your friends who want to learn Python programming from scratch to advanced.
Other string methods
For More Information:- Click Here