Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Python strip() Method Syntax
3.
Purpose of the strip() function in Python?
4.
Working of the strip() method
5.
String strip() in Python Example
5.1.
Python
6.
Types of strip() Function
7.
Implementation of the strip() Function
7.1.
Example 1: Removing leading and trailing whitespace
7.2.
Python
7.3.
Example 2: Removing specific characters from a string
7.4.
Python
7.5.
Example 3: Removing middle characters from a string
7.6.
Python
8.
Frequently Asked Questions
8.1.
What does S strip () do in Python?
8.2.
What does strip () split () do in Python?
8.3.
What is strip method?
8.4.
Why input () strip () is used?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Strip Function In Python

Author Gaurav Singh
1 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

String strip() is an inbuilt function in Python programming. The python strip() method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).

strip function in python

Python strip() Method Syntax

Here is the syntax of the method strip() in Python which can be used for removing the white spaces from the given string:

string.strip([chars])

 

In the syntax of the strip() function, an array of characters can be passed (optional); we can pass a set of characters to be removed from the left or right part of the string.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Purpose of the strip() function in Python?

The strip() function is one of the built-in functions in Python for string operations. It removes any leading (any whitespace before the text) and trailing (any whitespace after the text) white spaces that might be present in the given string. It helps us pre-process the text by removing unnecessary whitespace in the text.

Whitespaces are the default character to remove when using the strip() function. However, we can change that by defining any character bounded by inverted commas. We will try to understand more about this in the example sections, where we will see all cases where the strip() function could be used.

Working of the strip() method

strip() method is a built-in Python method that removes any whitespace before or after the text. It truncates the whitespace from the string. The method does not modify the original string instead creates a new string without the whitespace. The methods can work without the parameters and optional, you can also provide a set of characters that you want to be removed from the string as a parameter. The strip () method only removes the whitespace from the beginning if the characters you passed in the parameter match with the characters in the beginning. It works same for the characters in the right. 

String strip() in Python Example

Below is an example of how one could use the strip() function in Python.

  • Python

Python

text = "  Coding Ninjas      "
print(text)

stripped_text = text.strip()
print("Stripped Text:", stripped_text)

 

Output

Coding Ninjas      
Stripped Text: Coding Ninjas

Types of strip() Function

Apart from the strip() function, we have 2 more functions that strip the text either from the right or the left of the given string. They are:

  1. lstrip() - removes only leading whitespaces (any character after the string) from the string
  2. rstrip() - removes only trailing whitespaces (any character after the string) from the string

Implementation of the strip() Function

Example 1: Removing leading and trailing whitespace

  • Python

Python

string = "          This string has both leading and trailing whitespaces.          "
print(string.strip())

 

Output

This string has both leading and trailing whitespaces.

We can see that since there was no parameter entered along with the strip() function, it by default assumed that whitespaces have to be removed from the trailing and leading positions from the string, which we can see in the output.

Example 2: Removing specific characters from a string

  • Python

Python

string = "This strisng has some specific characters."
print(“Stripped String:”, string.strip("T.s"))

 

Output

Stripped String: his string has some specific character

We see that the characters that were passed into the strip() function's optional parameters were “T”, “.” and “s”. And only those characters were removed which were present on either the left end or the right end, as can be seen in the output. So now we know that strip() function can’t be used for removing middle characters but only leading or trailing characters from the string.

Example 3: Removing middle characters from a string

Since it's not possible to remove middle characters using the strip function we use another built-in function from Python called replace() function which takes in 2 parameters - (what_to_replace, what_to_replace_it_with)

  • Python

Python

string = "xxxxxThis is a string with xx spacesxxxxx"
string = string.replace("x", "") # Removes all 'x' characters from the string
print(string)

 

Output

This is a string with  spaces

Frequently Asked Questions

What does S strip () do in Python?

The strip() function in Python helps us to remove trailing and leading whitespaces or any defined character in the strip() function from the given text. However, when the parameters are not given, by default strip() function will assume whitespaces are what it has to strip.

What does strip () split () do in Python?

Strip() and split() are different Python built-in methods performed on strings. The strip() method in Python is used to remove the whitespace or the characters that are passed in the argument. The split() method, on the other hand, is used to split the string into substrings. By default, it considers whitespace as a separator. You can pass any custom argument as a delimiter. 

What is strip method?

The strip() method is one of the built-in functions in Python for string operations. It removes any leading (any whitespace before the text) and trailing (any whitespace after the text) white spaces that might be present in the given string.

Why input () strip () is used?

The input() strip() method is used when you want the user to enter the input string. Using the input() strip() method helps to remove the extra whitespace entered by the user. This method automatically removes the extra space and displays the string by removing any leading whitespace.  

Conclusion

The strip() function is a built-in function in Python Programming Language. It removes specific characters from leading and trailing positions in the given text. The data pre-processing step cleans the data for building high-end Machine Learning models. Thus, it provides excellent help by removing the whitespaces or specifically defined characters in the string. 

To learn more about how to build your career in programming, click here.

Recommended Readings:

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Previous article
Python Factorial
Next article
Round() function in Python
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass