Skip to content
Related Articles

Related Articles

Python string | strip()

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 18 Oct, 2022
Improve Article
Save Article

Python String strip() is an inbuilt function in the Python programming language that returns a copy of the string with both leading and trailing characters removed (based on the string argument passed). 

Syntax: 

string.strip([chars])

Parameter: 

There is only one optional parameter in it. chars – a string specifying the set of characters to be removed. If the optional chars parameter is not given, all leading and trailing whitespaces are removed from the string.

Return Value:

Returns a copy of the string with both leading and trailing characters removed.

What Is the Purpose of the Python Strip() Function?

When a developer wishes to remove characters or whitespaces from the beginning or end of a string, the Strip() function in Python comes in handy. Let’s take a closer look at it: 
a) The strip() function assists in removing characters from the beginning or end of a string for characters supplied as arguments to the strip() function ().

b) If the string has no whitespaces and the characters argument is not supplied, the string is returned as is.

c) It is also beneficial to eliminate whitespaces from the beginning and end of the text.

d) If the string contains whitespaces and no character arguments are supplied, the string will be returned after discretizing the whitespaces.

Example 1

Python3




# Python3 program to demonstrate the use of
# strip() method 
 
string = """    geeks for geeks    """
 
# prints the string without stripping
print(string)
 
# prints the string by removing leading and trailing whitespaces
print(string.strip())  
 
# prints the string by removing geeks
print(string.strip(' geeks'))

Output: 

    geeks for geeks     
geeks for geeks
for

Example 2

Python3




# Python Program to demonstrate use of strip() method
 
str1 = 'geeks for geeks'
# Print the string without stripping.
print(str1)
 
# String whose set of characters are to be
# remove from original string at both its ends.
str2 = 'ekgs'
 
# Print string after stripping str2 from str1 at both its end.
print(str1.strip(str2))

Output: 

geeks for geeks
 for

Working of the above code: 

  • We first construct a string str1 = ‘geeks for geeks’
  • Now we call the strip method over str1 and pass str2 = ‘ekgs’ as the argument. 
  • Now python interpreter trace str1 from left. It removes the character of str1 if it is present in str2. 
  • Otherwise, it stops tracing. Now python interpreter trace str1 from the right. It removes the character of str1 if it is present in str2. 
  • Otherwise, it stops tracing. Now at last it returns the resultant string. When we call strip() without argument, it removes leading and trailing spaces.

Example 3

Python3




# Python Program to demonstrate use of strip() method without any argument
str1 = """    geeks for geeks    """
 
# Print the string without stripping.
print(str1)
 
# Print string after removing all leading
# and trailing whitespaces.
print(str1.strip())

Input: 

    geeks for geeks     

Output: 

geeks for geeks

Example 4: Practical Application

 Given a string remove the occurrence of the word “the” from the beginning and the end.  

Python




# Python3 program to demonstrate the practical application
# strip()
 
string = " the King has the largest army in the entire world the"
 
# prints the string after removing the from beginning and end
print(string.strip(" the"))

Input: 

the King has the largest army in the entire world the

Output: 

King has the largest army in the entire world

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!