Table of Contents Hide
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.
In this article, we will learn about the Python String
strip() method with the help of examples.
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 Python String rstrip() method.
The Syntax of
strip() method is:
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.
strip() Return Value
strip() method returns a copy of the string by stripping both leading and trailing characters based on the arguments passed.
- 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 are the various working example of the
strip() method. We can use it to remove whitespace or we can strip the characters at both leading and trailing end.
text1.strip()– Removes the whitespace at both leading and trailing of the string
text3.strip(' code')– Remove the whitespace and the substring code from both leading and trailing end
text2.strip('code')– Removes only the substring from both 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'))
Python Programming its my its my Coding is fun
Example 2 – How to use strip() method in real world?
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, 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.