How to use the Python strip method – with examples

  • Reading time:16 mins read

What is a Python strip() method?

The Python Strip method returns a copy of a string by removing whitespaces by default if no argument was specified or by removing trailing and leading characters that have been specified as arguments of the method.

 

 

Although the Python strip method can be used plainly, it is further classified into two forms depending on whether we want to remove characters from the left or the right side.

The “rstrip” and “lstrip” are used to remove characters from the right side and left side respectively. We can also say that the “rstrip” and “lstrip” methods are used to remove trailing and leading characters respectively.

 

Python strip

 

The plain Python strip method is widely used to remove white spaces from any strings that have probably been picked up from outside the code and need tidying up.

Using a string named “mystr” that we have modified to contain whitespaces the code below shows how we can use the plain strip method to remove leading and trailing whitespace.

We are also going to print out the original string alongside the new string that has no white spaces so that we can note the difference.

 


Now the Python strip method is particularly useful if we had certain characters at the beginning and at the end of the strings that we wanted to be removed.

Note that the first line of the output contains the original string that has not been stripped of the whitespaces while the second line is the new string that has been stripped off the whitespaces.

For instance, if we wanted to remove the L in the word Learn we can then pass it as an argument within the strip method enclosed by quotation marks and this is going to remove the character from the string.

 

 

Although we end up with a string that is incomprehensible we can actually see that the Python strip method removes the letter L from the string.

 

Python strip

 

We can also remove the letter n from the right side of the string by specifying the letter n as an argument within the Python strip method as well.

 

 

Using the Python strip method we can also format the second string my_url.

 

 

Let’s say that we want to remove the HTTPS part so that we can just have codeberryschool.com bit maybe it is easier for presenting or something like that.

 

 

Note that we don’t have to repeat the ‘s’ and the forward slash in the https://, we only need to state the character that we want to be removed once.

Suppose we had a trailing slash at the end of the string my_url, if we re-run the code above, the trailing forward slash will also be removed alongside the https://

 

 

Suppose we never want to remove the trailing slash alongside the https://  in this case we need to use the “lstrip” method instead of the general Python strip method.

 

Python strip

 

This allows us to remove only characters that are on the left side or at the beginning of the string as shown below.

 

 

We can also use the rstrip() in the same way to remove leading characters or characters at the right side of the string.

For instance, we can remove the forward slash at the end of string my_url using the “rstrip” method. This leaves similar characters at the beginning of the string untouched.

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.