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.
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.
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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
