I'm not gonna lie. There are multiple ways you can trim a string in Python.
But... the truth is, you don't need to know every one of them.
In this article, you'll see only the most important techniques, such as stripping leading and trailing spaces (as well as the ones inside the string). You'll also learn how to remove tabs, newlines, carriage return (CRLF), and other characters. And we'll be using nothing more than native methods and regex—no external libraries required!
By the end of this article, you'll have mastered:
Trimming a string means deleting certain chars from the start, the end, or both sides of a string. Removing unwanted chars makes it easier to compare strings and can prevent hard to debug issues.
You can remove any kind o character, but usually what we're interested in is deleting blank spaces, new lines, carriage return (CRLF), tabs and other special symbols.
In this section, we're going to see how to remove leading or trailing spaces, blank spaces, newline character, carriage return (CRLF), and tabs.
str class has a very convenient method to trim leading spaces named
str.lstrip, a shorthand for "left-strip", since it trims a string from the left-hand side. You can think of it as a left trim.
>>> ' hello '.lstrip() 'hello '
str.lstrip with no arguments, it removes all whitespaces from left to right. But if all you want is to strip the first char, then there are two ways of doing this. The first one assumes that there will always be at least one whitespace in the beginning of the string. If that's the case, then you can just slice it.
>>> s = ' hello' >>> s = s[1:] >>> s ' hello'
If there's no guarantee of that, we'll need to check first if the string starts with space.
>>> def strip_first(s: str, ch: str = ' ') -> str: if s and s == ch: return s[1:] return s >>> strip_first('hello') 'hello' >>> strip_first(' hello') ' hello'
The way to remove trailing spaces from the end of the string is to use
This method expects a list of chars and trims the string from the right. It removes all chars that match one of those you passed, and stop as soon as it cannot match anymore. By default,
str.rstrip() removes blanks if you don't pass anything to it. You can think of it as a right trim.
>>> ' hello '.rstrip() ' hello' >>> '***hello***'.rstrip('*') '***hello'
Sometimes you might want to trim only the last character of a string. And we can use the same logic from the previous example. Check if the last char is a space, and use slice to remove it.
>>> def strip_last(s: str, ch: str = ' ') -> str: if s and s[-1] == ch: return s[:-1] return s >>> strip_last('hello') 'hello' >>> strip_last('hello ') 'hello' >>> strip_last('') ''
If all you want is to remove whitespaces from start and end of string,
str.strip will serve you better.
This method trims both sides of the string. And just like
str.rstrip, if you can pass any combination of chars as argument, it removes them from both ends.
⚠️ A common misconception is to think that there's a trim() function in Python.
# by default, strip removes whitespaces >>> ' hello '.strip() 'hello' # but you can also strip other character >>> '***hello***'.strip('*') 'hello'
We've seen how
str.strip can remove blank spaces from both sides of a string. I've also mentioned that this method takes a chars argument that you can use pass a combination of character you want to trim.
To trim line breaks, you can pass
\n and it will strip all newlines from both sides of the string.
>>> s = """ ... ... ... hello ... ... ... """ >>> s '\n\n\n hello\n\n\n' >>> s.strip('\n') ' hello'
The Carriage Return (CR), and Line Feed (LF) are nothing more than a newline character. They are represented by the concatenation of
\r\n. This is how Microsoft Windows, Symbian OS and other non-Unix operating systems represent a new line [source].
Removing them from a string is the same as removing the single newline. You feed
\r\n and method does its job!
>>> s = " hello world\r\n\r\n" >>> print(s) hello world >>> s.strip('\r\n') ' hello world'
If you are following this guide from the beginning you might already know how to do this. Trimming tabs from a string in Python is the same as other characters, you use
str.strip and pass the '\t' string to it.
>>> s = "\t\t\t hello world \t" >>> s '\t\t\t hello world \t' >>> print(s) hello world >>> s.strip('\t') ' hello world '
And that's it!
As I mentioned before,
str.strip takes as argument a string, not just a single char. This sequence of chars is a combination of all chars you want to remove from the beginning and end of your string.
>>> s = " \ns hello world \n s" >>> s ' \ns hello world \n s' >>> print(s) s hello world s >>> s.strip('\n s') 'hello world'
Sometimes you want to do more than trimming, let's say you want to remove chars inside the string. There are two ways of doing this: one is to remove only the duplicates; the other is to remove all extra spaces.
To remove only the duplicated characters, you can use the regex module
>>> import re >>> s = " Python is really a great language. " >>> re.sub("\s+" , " ", s) ' Python is really a great language. '
This method gets rid of all consecutive spaces. What if you want to do not only that, but also trim the string by removing the leading and trailing blanks?
One way is to split the string and then joining then like so:
>>> s = " Python is really a great language. " >>> " ".join(s.split()) 'Python is really a great language.' >>> # This is the same as using regex then stripping the whitespaces >>> re.sub("\s+" , " ", s).strip() 'Python is really a great language.'
Now, if you want to strip all whitespace in your string, either use regex or call the
>>> import re >>> s = " Python is really a great language. " >>> re.sub("\s+" , "", s) 'Pythonisreallyagreatlanguage.'
>>> s = " Python is really a great language. " >>> s.replace(' ', '') 'Pythonisreallyagreatlanguage.'
Trimming a list of strings is almost the same as trimming an individual one. The only difference is that you have to iterate over the list, and call
str.strip method on each one. You do so by using a list comprehension, for example, to return a new list with all strings trimmed.
>>> lst = ["string1\n", "string2\n", "string3\n"] >>> [s.strip('\n') for s in lst] ['string1', 'string2', 'string3']
It's very common to use Numpy for data science tasks due to its performance and ease to use.
If you have a array of strings and want to trim each one of them, Numpy comes with an efficient vectorized implementation of
In fact, it also has
.replace, and many other string operations.
The vectorized versions work slightly differently, they are not a method but a function in the
numpy.char module. So you must pass the array and the list of chars you want to trim.
>>> import numpy as np >>> arr = np.array([' helloworld ', ' hello']) array([' helloworld ', ' hello'], dtype='<U7') >>> np.char.strip(arr, ' ') array(['helloworld', 'hello'], dtype='<U7')
In this post, you learned several ways of trimming a string in Python, including array of strings. Python allows us to strip leading and trailing characters easily. And if instead of removing the extra chars on each side you want to remove the ones internally, you can count on the regex module. I hope you've found this article helpful and see you next time!
Other posts you may like:
See you next time!
This post was originally published at https://miguendes.me