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
text = " Coding Ninjas "
print(text)
stripped_text = text.strip()
print("Stripped Text:", stripped_text)

You can also try this code with Online Python Compiler
Run Code
Output
Coding Ninjas
Stripped Text: Coding Ninjas

You can also try this code with Online Python Compiler
Run Code
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:
- lstrip() - removes only leading whitespaces (any character after the string) from the string
- 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
string = " This string has both leading and trailing whitespaces. "
print(string.strip())

You can also try this code with Online Python Compiler
Run Code
Output
This string has both leading and trailing whitespaces.

You can also try this code with Online Python Compiler
Run Code
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
string = "This strisng has some specific characters."
print(“Stripped String:”, string.strip("T.s"))

You can also try this code with Online Python Compiler
Run Code
Output
Stripped String: his string has some specific character

You can also try this code with Online Python Compiler
Run Code
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
string = "xxxxxThis is a string with xx spacesxxxxx"
string = string.replace("x", "") # Removes all 'x' characters from the string
print(string)

You can also try this code with Online Python Compiler
Run Code
Output
This is a string with spaces

You can also try this code with Online Python Compiler
Run Code
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!