CodeNewbie Community 🌱

Cover image for 15 Easy Ways to Trim a String in Python
Miguel Brito
Miguel Brito

Posted on • Originally published at miguendes.me

15 Easy Ways to Trim a String in Python

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:

How to Trim Characters From a String

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.

Stripping Leading Whitespace From Beginning of a String

The 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   '
Enter fullscreen mode Exit fullscreen mode

using python .lstrip method to remove leading spaces from a string

When calling 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'
Enter fullscreen mode Exit fullscreen mode

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[0] == ch:
         return s[1:]
     return s

>>> strip_first('hello')
'hello'

>>> strip_first('   hello')
 '  hello'
Enter fullscreen mode Exit fullscreen mode

Stripping Trailing Whitespace From End of a String

The way to remove trailing spaces from the end of the string is to use str.rstrip.

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'
Enter fullscreen mode Exit fullscreen mode

using python .rstrip method to remove trailing spaces from the end of a string

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('')
''
Enter fullscreen mode Exit fullscreen mode

Removing Spaces From From Start and End of a String

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.lstrip and 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'
Enter fullscreen mode Exit fullscreen mode

using python .strip to remove spaces from both sides of a string

How to Trim Newlines

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'
Enter fullscreen mode Exit fullscreen mode

strip new lines from a string using .strip method

How to Trim Carriage Return (CRLF)

The Carriage Return (CR), and Line Feed (LF) are nothing more than a newline character. They are represented by the concatenation of \r and \n forming \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 str.strip with \r\n and method does its job!

>>> s = "  hello world\r\n\r\n"
>>> print(s)
  hello world


>>> s.strip('\r\n')
'  hello world'
Enter fullscreen mode Exit fullscreen mode

trimming carriage return - line feed (CRLF) from a string in python

How to Trim Tabs

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 '
Enter fullscreen mode Exit fullscreen mode

stripping tabs from a string using strip method in python

And that's it!

How to Trim a Combination of Characters From a String

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'
Enter fullscreen mode Exit fullscreen mode

trimming a combination of more than one char from both sides of a string in python

How to Remove Multiple Spaces Inside a String

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.

Removing Only Duplicates

To remove only the duplicated characters, you can use the regex module re

>>> import re
>>> s = "   Python   is really   a    great language.    "
>>> re.sub("\s+" , " ", s)
' Python is really a great language. '
Enter fullscreen mode Exit fullscreen mode

removing duplicate spaces in a string in python using re module

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.'
Enter fullscreen mode Exit fullscreen mode

Removing All Spaces

Now, if you want to strip all whitespace in your string, either use regex or call the str.replace method.

Using re (regex module)

>>> import re
>>> s = "   Python   is really   a    great language.    "
>>> re.sub("\s+" , "", s) 
'Pythonisreallyagreatlanguage.'
Enter fullscreen mode Exit fullscreen mode

removing all spaces from a string in python using regex

Using replace

>>> s = "   Python   is really   a    great language.    "
>>> s.replace(' ', '')
'Pythonisreallyagreatlanguage.'
Enter fullscreen mode Exit fullscreen mode

removing all spaces from a string using .replace

How to Strip a List of Strings

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']
Enter fullscreen mode Exit fullscreen mode

How to Strip an (Numpy) Array of Strings

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 strip.

In fact, it also has .lstrip, .rstrip, .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')
Enter fullscreen mode Exit fullscreen mode

python_numpy_1.png

Conclusion

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!

References:

https://stackoverflow.com/questions/761804/how-do-i-trim-whitespace-from-a-string

https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string

https://stackoverflow.com/questions/1546226/is-there-a-simple-way-to-remove-multiple-spaces-in-a-string

Other posts you may like:

See you next time!

This post was originally published at https://miguendes.me

Top comments (1)

Collapse
 
neeljyo profile image
Neel Jy

Quite insightful blog. I have refered a python interview question for preparing for my job interview.