Python String strip()
Python String strip() method
Python String strip() method returns a string with the whitespaces or specified characters removed from the leading (beginning) and trailing (ending) edges of given string.
Consider that given string is in x.
x = " hello world "
Then the return value of x.strip() is
"hello world"
In this tutorial, you will learn the syntax and usage of String strip() method in Python language.
Syntax of strip() method
The syntax of String strip() method in Python is given below.
str.strip(chars)
Parameters
The string strip() method takes a single parameter.
Parameter | Description |
---|---|
chars | Optional These specified characters are removed from the edges of the string. Default value is None. |
Return value
The string strip() method returns a String value.
Examples
In the following examples, you will learn how to use strip() method to strip whitespaces from the string, and how to use strip() method to strip specified characters from the string.
1. Strip whitespaces from string in Python
In this example, we take a string value in variable x, where string contains some whitespaces at the beginning and ending of the string. We have to remove these whitespaces present at the edges of the string.
Steps
- Given a string in x.
- Call strip() method on the string x. By default, when no argument is specified, the strip() method removes whitespaces characters from the edges of the string.
x.strip()
- The strip() method returns a new string, created from removing the whitespace characters from the original string. Assign the returned value to a variable, say x_stripped.
x_stripped = x.strip()
- You may print the stripped string to output.
Program
The complete program to strip whitespace characters from the edges of given string.
Python Program
# Given string
x = " hello world "
# Strip whitespaces
x_stripped = x.strip()
print(f"Original : \"{x}\"")
print(f"Stripped : \"{x_stripped}\"")
Output
Original : " hello world "
Stripped : "hello world"
Please note that the whitespace characters which are not at the edges are present in the output string, like the single space between hello and world.
2. Strip specified characters from string in Python
Instead of whitespace characters, we can also specify characters via argument to strip() method, to strip from the string.
In this example, we take a string value in variable x, and we have to remove the characters ".-,"
[dot, hyphen, comma] from the edges of the string in x.
Steps
- Given a string in x.
- Call strip() method on the string x and pass
".-,"
for the argument to the strip() method.
x.strip(".-,")
- The strip() method returns a new string, created from removing the specified characters, which are dot, hyphen, and comma. Assign the returned value to a variable, say x_stripped.
x_stripped = x.strip()
- You may print the stripped string to output.
Program
The complete program to strip specified characters from the edges of given string.
Python Program
# Given string
x = "......,,hello world----...-"
# Strip specified characters
x_stripped = x.strip(".-,")
print(f"Original : \"{x}\"")
print(f"Stripped : \"{x_stripped}\"")
Output
Original : "......,,hello world----...-"
Stripped : "hello world"
Summary
In this tutorial of Python String Methods, we learned about String strip() method, its syntax, and examples.