Python strip functions remove characters or white spaces from the beginning or end of a string. The strip functions are strip(), lstrip(), and rstrip(). They remove characters from both ends of a string, the beginning only, and the end only. Python strip() can also remove quotes from the ends of a string.
You may have white spaces that you want to remove from the ends of the string. For example, you may have a name with spaces at the end that you want to remove before your program continues.
Python includes three built-in functions that can be used to trim the white spaces from a string and return a new string: strip(), lstrip(), and rstrip().
These methods can be useful if you need to validate the structure of a string. They are also useful if you need to remove additional white spaces that you do not want to be included in your string.
In this tutorial, we are going to discuss how to use the Python string strip() method and its counterparts, lstrip() and rstrip().
Python String Strip
The Python strip() method removes any spaces or specified characters at the start and end of a string. strip() returns a new string without the characters you have specified to remove.
The syntax for the strip() method is:
" TEST ".strip()
This example removes all the leading and trailing white space characters in our string.
Strip Characters from String Python
Let’s say that we have a name with spaces at the end that we want to remove. Here’s an example of the Python strip() method being used to remove those spaces:
name = "John Appleseed " print(name.strip())
Our code returns the following Python string: John Appleseed. We have removed the white space leading and trailing characters. The strip() method returns a copy of the string so our original string is unmodified.
The string strip() function by default removes the leading and trailing white space characters from the end of a string. We can use the optional character argument if we want to remove a specific character from the start and end of a string.
Here’s an example of a strip() function that removes the letter X from the start and end of a string:
sentence = "XXThis is an example sentence.XX" print(sentence.strip("X"))
Our program removes any instances of the letter X at the start and end of our string. Our character strip returns:
This is an example sentence.
Similarly, we can use the optional argument we discussed above to remove multiple characters at the start and end of a string. If we wanted to remove both X and Y from the start and end of our string, we could use a program like this:
sentence = "XYThis is an example sentence.XY" print(sentence.strip())
Our code returns the following: This is an example sentence.
Python strip() is particularly useful for removing quotes at both ends of a string. If strip() is given the argument of single quotes ‘ or double quotes “ it will remove those from both ends of a string.
Python Strip Characters: lstrip() and rstrip()
The Python strip() function removes specific characters from the start and end of a string. But what if you want to remove characters from only the start or end of a string? That’s where the Python lstrip() and rstrip() methods come in.
The Python lstrip() method removes the leading spaces—or A leading character you specified— from the left side of a string. The rstrip() method removes trailing spaces or characters from the right side of a string.
The program will remove all white space characters by default if you do not specify a character to remove.
Python Strip String: lstrip() and rstrip() Example
So, let’s say that we have an invoice heading that includes asterisks on the right side of the string that we want to remove.
Let’s assign a string to a Python variable and trim all the asterisks from its right side:
invoice_heading = "Heading*****" print(invoice_heading.rstrip("*"))
Our code returns: Heading. As you can see, our program has removed all the asterisks on the right side of our string. However, if we also had asterisks at the start of our string, our program would not remove them. Here’s an example to illustrate this effect:
invoice_heading = "*****Heading*****" print(invoice_heading.rstrip("*"))
The rstrip() function will remove all the asterisks on the right side of our string but not those on the left:
*****Heading
Similarly, you can use the lstrip() function in Python to remove specific characters from the start of a string:
invoice_heading = "*****Heading*****" print(invoice_heading.lstrip("*"))
Our code returns the following:
Heading*****
Conclusion
The Python string strip() function removes characters from the start and/or end of a string. By default, the strip() function will remove all white space characters—spaces, tabs, new lines.
But, you can specify an optional character argument if there’s a specific character you want to remove from your string.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
In this guide, we discussed the basics of strings in Python, and how to use the strip(), lstrip(), and rstrip() functions to remove characters from the start and/or end of a string. We also explored a few examples of each of these functions in action.
Now you’re ready to use the Python strip(), lstrip(), and rstrip() methods like an expert! For more learning resources on the Python language, check out our How to Learn Python guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.