Signup/Sign In

Python String strip()

The Python strip() string method is used to remove/strip any given character or substring from the leading and trailing characters(substring) of a given string.

  • This method only removes the left and right side characters, that too only if they matches the argument provided in the strip() method and returns the copy of the string.

  • If we'll not pass any character to the current method then it'll take away all the leading and trailing whitespace from the string.

  • To remove whitespaces or any special character, or any prefix or suffix from a given string this method is employed.

  • The matching is case sensitive, which means study and Study are treated as different.

For example, if I have a user input where user can input their fullname, then we can use the strip() method to remove any prefix added to the full name. Many individuals add Mr. or Miss. before their fullname. So we can use the strip() function like strip("Mr.") or strip("Miss."), like this, to sanitize the input string.

Similarly, we can use it to remove empty spaces added before and after a string.

Python String strip(): Syntax

Below we've got the basic syntax of the strip() function in Python:

string.strip([character])

Python String strip(): Parameters

Given below is the parameter of strip() function:

  • character

    This is an optional parameter used to indicate the characters that are needed to be removed from the string. If this parameter isn't provided, in those cases this method can take away all leading and trailing whitespaces from the string. In other words, the default value for this parameter is whitespace.

Python String strip(): Returned Values

This method returns the string after removing the leading and trailing characters removed from the string.

Python String strip(): Basic Example

Below we have an example to show the working of the strip() function:

str = "   StudyTonight!! Best place to learn coding"
print(str.strip());

The output for the same is given below:


StudyTonight!! Best place to learn coding

It is concluded from the above example that space before the character "S" is removed in the output.

Python String strip(): Removing characters from a String

There is an example given below where we remove the characters from a string:

str = "StudyTonight!!Best place to learn coding for online study"
print(str.strip("Study"));

The Output for the same is given below:


Tonight!!Best place to learn coding for online study

Note: It is important to remember if the combination of characters in the parameter mismatches with the characters in the string then it will stop striping the characters. For this case, we have an example too and the code snippet for the same is given below:

str="StudyTonight!!Best place to learn coding"
print(str.strip("study"));

The Output for the same is given below:


StudyTonight!!Best place to learn coding

From the above output, we are hoping that it is clear to you if the character in the string and character in the passed parameter mismatches then strip() function is unable to perform its function.

Time for a Live Example!

Let us see the example given below where we will see the strip() function in different situations:

Summary

In this tutorial, we learned that to remove leading and trailing characters from the specified string, we can use the strip() method of Python.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.