In this article, we'll examine how to strip characters from both ends of a string in Python.
The built-in String
type is an essential Python structure, and comes with a built-in set of methods to simplify working with text data. There are many situations in which a programmer may want to remove unwanted characters, i.e. strip certain characters, from either the beginning or ending of a string.
The most common requirement is to strip whitespace (spaces, tabs, newline characters, etc.) from both ends of a string. This usually occurs after importing raw text data from a file, database, web service, or after accepting user input, which may contain typos in the form of extra spaces. This can be handled by the default usage of the String.strip()
method, as seen here:
>>> orig_text = ' The cow jumped over the moon! \n'
>>> print(orig_text.strip())
The cow jumped over the moon!
>>>
Note that this method does not change the original value of the string, i.e. it does not change the string in-place. It simply returns a new string with the whitespace on either end stripped out. We can verify this by printing out the original string:
>>> print(orig_text)
The cow jumped over the moon!
>>>
The strip
method also enables us to specify which types of characters we want to strip. This can be useful if we want to remove other characters besides whitespace. To do this we simply specify the characters to strip by passing an argument containing these characters to the String.strip()
method:
>>> orig_text = '-----The cow jumped over the moon!$$$$$'
>>> print(orig_text.strip('-$'))
The cow jumped over the moon!
>>>
This is useful for removing characters at the start or end of a string that were used for formatting purposes, for example. So if you have a Markdown-formatted string, you can easily remove the header syntax like this:
>>> md_text = '### My Header Here' # Denotes an H3 header in Markdown
>>> print(md_text.strip('# '))
My Header Here
>>>
Finally, Python provides a way to strip characters from only one side of the string via the String.rstrip()
and String.lstrip()
methods. These methods work exactly the same way as the String.strip()
method, but String.rstrip()
only removes characters from the right side of the string and String.lstrip()
only removes characters from the left side of the string:
>>> orig_text = '*****The cow jumped over the moon!*****'
>>> print(orig_text.rstrip('*'))
*****The cow jumped over the moon!
>>> print(orig_text.lstrip('*'))
The cow jumped over the moon!*****
Once again we can print the original string to see that it was unaffected by these operations:
>>> print(orig_text)
*****The cow jumped over the moon!*****
About the Author
This article was written by Jacob Stopak, a software consultant and developer with passion for helping others improve their lives through code. Jacob is the creator of Initial Commit - a site dedicated to helping curious developers learn how their favorite programs are coded. Its featured project helps people learn Git at the code level.