Python String strip()

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.

strip() Syntax

The Syntax of strip() method is:

string.strip([chars])

strip() 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. 

strip() 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 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'))

Output

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.

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++']
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy Repeat

NumPy Repeat

Table of Contents Hide NumPy Repeat SyntaxNumPy Repeat ParametersNumPy Repeat Return ValueExample 1: Repeating the Single Dimensional NumPy ArrayExample 2: Repeating the Two-Dimensional NumPy ArrayExample 3: Repeating the NumPy Array…
View Post
Numpy.argmax() In Python

numpy.argmax() in Python

Table of Contents Hide SyntaxParametersReturn ValueFinding the maximum element from a matrix with Python numpy.argmax()Using np.unravel_index on argmax outputFinding Maximum Elements along columns using Python numpy.argmax() The numpy.argmax() function returns the indices…
View Post