What is Python Strip?

The strip() function is part of the built-in function available in python. The function will remove given characters from the start and end of the original string.

By default, function strip() will remove white spaces from the start and end of the string and return the same string without white spaces.

In this Python tutorial, you will learn:

Syntax

string.strip([characters])

Parameters

  • characters : (optional) The given characters will be removed from the start or end of the original string.
  • If the characters parameter is not given, the whitespaces from the start and end of the string will be removed.

Return Value

The strip() will return:

  • The original string with white spaces removed from the start and end if the characters to be removed are not specified.
  • In case the string does not have any whitespaces at the start or end, the string will be returned as it is and will match with the original string.
  • If the characters parameter is given, and if the characters are given matches, the characters at the start or end of the string will be removed from the original string, and the rest of the string will be returned.
  • Incase if the characters given does not match the start or the end in the original string, the string will be returned as it is.

Example 1: Strip Method

str1 = "Welcome to Guru99!"
after_strip = str1.strip()

Output:

Welcome to Guru99!

Example 2: Strip on Invalid Data Type

The strip() function works only on strings and will return an error if used on any other data type like list, tuple, etc.

Example when used on list()

mylist = ["a", "b", "c", "d"]
print(mylist.strip()) 

The above will throw an error :

Traceback (most recent call last):
  File "teststrip.py", line 2, in <module>
    print(mylist.strip())
AttributeError: 'list' object has no attribute 'strip'

Example 3: Without character parameter

str1 = "Welcome to Guru99!"
after_strip = str1.strip()
print(after_strip)

str2 = "Welcome to Guru99!"
after_strip1 = str2.strip()
print(after_strip1)

Output:

Welcome to Guru99!
Welcome to Guru99!

Example 4: Passing character parameter

str1 = "****Welcome to Guru99!****"
after_strip = str1.strip("*")
print(after_strip)

str2 = "Welcome to Guru99!"
after_strip1 = str2.strip("99!")
print(after_strip1)
str3 = "Welcome to Guru99!"
after_strip3 = str3.strip("to")
print(after_strip3)

Output:

Welcome to Guru99!
Welcome to Guru
Welcome to Guru99!

Why Python strip function is used?

Here, are reasons for using Python strip function

  • It helps to remove the characters at the start of the string and also at the end of the string based on the characters given to be removed from the original string.
  • If the characters given do not match the original string, the string will be returned as it is.
  • If the characters to be removed are not specified, then the whitespaces from the start and end of the original string will be removed.
  • If there is no white space at the start or end than the string will be returned as it is.

Summary:

  • The strip() function is part of the built-in function available in python. The function will remove given characters from the start and end of the original string.
  • This function is very helpful in removing the whitespaces at the start and end of the given string, as shown in the example.
  • It helps to remove the characters at the start of the string and also at the end of the string based on the characters given to be removed from the original string.
  • If the characters given do not match the original string, the string will be returned as it is.
  • If the characters to be removed are not specified, the whitespaces from the start and end of the original string will be removed.
  • If there is no white space at the start or end than the string will be returned as it is.

 

YOU MIGHT LIKE: