The Python String strip() method is a built-in function that strips both leading and trailing characters based on the arguments passed to the function and returns the copy of a string.
If we want to remove only the leading characters in a string, we could use Python String lstrip(). Similarly, if we want to strip only the trailing characters, we could use the Python String rstrip() method.
Syntax
The Syntax of strip()
method is:
string.strip([chars])
Parameters
The strip()
method takes one parameter, and it’s optional.
- chars(optional) – set of characters representing string that needs to be removed from both left and right-hand sides of the string.
If the chars argument is not passed, the strip()
function will strip whitespaces at the start and end of the string.
Return Value
The strip()
method returns a copy of the string by stripping both leading and trailing characters based on the arguments passed.
Note:
- If we do not pass any arguments to
strip()
function, by default, all the leading and trailing whitespaces are truncated from a string. - If the string does not have any whitespaces at the start or end, the string will be returned as-is, matching the original string.
- If the characters passed in the arguments do not match the characters at the beginning of the string, it will stop removing the leading characters.
- Similarly, if the characters passed in the arguments do not match the characters at the end of the string, it will stop removing the trailing characters.
Example: Working of the strip() method
Below is the various working example of the strip()
method. We can use it to remove whitespace or we can strip the characters at both the leading and trailing ends.
text1.strip()
– Removes the whitespace at both the leading and trailing of the stringtext3.strip(' code')
– Remove the whitespace and the substring code from both the leading and trailing endtext2.strip('code')
– Removes only the substring from both the leading and trailing end.text4.strip('The')
– Removes the substring only at the leading end.
# Leading and trailing whitespaces are removed
text1 = ' Python Programming '
print(text1.strip())
# Remove the whitespace and specified character on
# both leading and trailing end
text3 = ' code its my code '
print(text3.strip(' code'))
# Remove the specified character at
# both leading and trailing end
text2 = 'code its my code'
print(text2.strip('code'))
# strips only the beginning
# of the string
text4="The Coding is fun"
print(text4.strip('The'))
Output
Python Programming
its my
its my
Coding is fun
Example 2: Python strip() method usage in actual program
Suppose we are fetching data from external sources like Excel, DB, or third-party APIs. In that case, there are higher chances that data is not formed correctly, and we may get the separators like pipe, comma, a hyphen, etc., appended to the string. We can use the strip() method to remove those special characters and preserve the original value.
In the below example, we have a list of languages and the hyphen is appended at both the trailing and leading end of each element. We can simply iterate the list and remove the hyphen using the strip()
method as shown below.
langs = ['-Python-', '-Java-', '-Javascript-', '-C#-', '-C++-']
new_langs = []
for l in langs:
new_langs.append(l.strip('-'))
print(new_langs)
Output
['Python', 'Java', 'Javascript', 'C#', 'C++']