Tutorial Study Image

Python strip()

The strip() function in python removes the trailing and leading characters in the copy of the original string based on the arguments given. The method returns this copy as output.


string.strip([chars]) #where chars are those to remove from right & left
 

strip() Parameters:

The strip() function takes a set of characters as its parameter. If chars are not provided the trailing and leading whitespaces are removed from the string.

Parameter Description Required / Optional
chars characters to remove as trailing and leading characters Optional

strip() Return Value

The return value is always a string. Until the first match found, it searches from both left and right of the string and stops removing if it reaches the end of the string.

Input Return Value
string copy of string

Examples of strip() method in Python

Example 1: How strip() works in Python?


string1 = ",,,,,rrttgg.....python....rrr"
# Removing characters
string2 = string1.strip(",.grt")

print(string2)
 

Output:


python

Example 2: How strip() removes white spaces in both sides?


string1 = "  Hii Python!  "
# Removes white spaces
after_strip = string1.strip()
 

Output:


Hii Python!