Python Strip Method

Python Strip() Method for Strings

Table of Contents

The strip() function is an inbuilt function in Python used to remove defined characters from the start and end of the string. If, for instance, you have a string that contains some unwanted characters at the beginning or end of the string, the Strip() method in Python can take them off. In this tutorial, you will learn how to use python strip method with various examples. In straightforward terms, here’s what you will learn:

  • Strip() Method Syntax
  • Calling the Strip method with no Argument
  • Calling the Strip Method on Invalid DataTypes
  • Calling the Strip() method with defined characters. 

Let’s begin with the docstring of the function according to its official documentation.

Strip() Method Syntax

Signature: a.strip(chars=None, /)
Docstring:
Return a copy of the string with leading and trailing whitespace removed.
 
If chars are given and not None, remove characters in chars instead.
Type:      builtin_function_or_method

Returns:

The strip method returns:

  • In cases where no argument was passed, the string is stripped of whitespaces in the beginning and end of the string.
  • If there exists no whitespace at the beginning or end of the string, the original string will be returned.
  • When a character is passed on the method, the characters at the beginning or end of the string will be removed. In other words, the returned string will not contain the characters at the beginning or end of the string. 
  • If the character passed on the method is not found at the beginning or end of the string, the string is returned as the original string.

Calling the Strip method with no Argument

By default, the strip method removes whitespaces at the beginning or end of strings. In other words, if no argument is passed on the strip function, whitespaces are assumed. Let’s see an example. Let’s say we have a string like this ‘Hello world ‘, the strip method without passing an argument will return ‘Hello world’. Now we write the code.

#define some string
string = '         Hello world '
 
#call the strip method on the string and print the result
stripped_string = string.strip()
print(stripped_string)

Output:
Hello world

If we call the method on a string that has no whitespace in the beginning or end, the string is returned. See the example below. 

#define some string
string = 'Hello world'
 
#call the strip method on the string and print the result
stripped_string = string.strip()
print(stripped_string)
Output:
Traceback (most recent call last):
  File "c:/Users/DELL/Desktop/pycodes/__pycache__/strings.py", line 5, in <module>
    stripped_string = string.strip()
AttributeError: 'list' object has no attribute 'strip'

Calling the Strip() method with defined characters. 

The last case scenario will be passing a character as an argument. Let’s see some examples.

#define some string
string = '///Hello world///'
 
#call the strip method on the string and print the result
stripped_string = string.strip('/')
print(stripped_string)
Output:
Hello world

It can also be used to remove strings that exist at the end of another string. Say we wish to remove ‘Infosys’ from ‘H2kInfosys’; we can run the following code.

#define some string
string = 'H2kInfosys'
 
#call the strip method on the string and print the result
stripped_string = string.strip('Infosys')
print(stripped_string)
Output:
H2k

In summary, 

You have learned how to use the strip() method to remove characters from the tail or beginning a string. You have also seen coding examples that explain the use cases of the strip() function. If you’ve got any questions, please leave them in the comment section, and I’d do my best to answer them.

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class