Remove spaces, tabs and newlines from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove spaces, tabs and newlines from a String in Python #

Use the re.sub() method to remove the spaces, tabs and newlines from a string, e.g. result = re.sub(r'\s+', '', my_str). The re.sub() method will remove all spaces, tabs and newlines from the string by replacing them with emtpy strings.

main.py
import re my_str = ' hi there ' # ✅ Remove all spaces, tabs and newlines from string (re.sub) result = re.sub(r'\s+', '', my_str) print(result) # 👉️ 'hithere' # ✅ Remove all spaces, tabs and newlines from string (re.split()) result = ''.join(my_str.split()) print(result) # 👉️ 'hithere' # ✅ Remove leading and trailing spaces, tabs and newlines from string result = my_str.strip() print(result) # 👉️ 'hi there' # ✅ Replace multiple, consecutive spaces, tabs or newlines with a single space result = ' '.join(my_str.split()) print(result) # 👉️ 'hi there'

The first example uses the re.sub() method to remove the spaces, tabs and newline characters from the string.

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

main.py
import re my_str = ' hi there ' result = re.sub(r'\s+', '', my_str) print(result) # 👉️ 'hithere'

If the pattern isn't found, the string is returned as is.

The \s character matches unicode whitespace characters like [ \t\n\r\f\v].

The plus + is used to match the preceding character (whitespace) 1 or more times.

We remove all spaces, tabs and newline characters from the string by replacing them with empty strings.

Alternatively, you can use the str.split() and str.join() methods.

To remove all spaces, tabs and newlines from a string:

  1. Use the str.split() method to split the string on the characters.
  2. Use the str.join() method to join the list of strings.
  3. The new string won't contain any spaces, tabs and newline characters.
main.py
my_str = ' hi there ' result = ''.join(my_str.split()) print(result) # 👉️ 'hithere'

The str.split() method splits the string into a list of substrings using a delimiter.

When the str.split() method is called without a separator, it considers consecutive whitespace characters as a single separator.
main.py
my_str = ' hi there ' # 👇️ ['hi', 'there'] print(my_str.split())

When called without an argument, the str.split() method splits on consecutive whitespace characters (e.g. \t, \n, etc), not only spaces.

The next step is to use the str.join() method to join the list of strings without a separator.

main.py
my_str = ' hi there ' result = ''.join(my_str.split()) print(result) # 👉️ 'hithere'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

You can use the same approach if you need to replace multiple, consecutive spaces, tabs or newlines with a single space.
main.py
my_str = ' hi there ' result = ' '.join(my_str.split()) print(result) # 👉️ 'hi there'

We used a string containing a space as the separator when joining the elements.

If you need to remove the leading and trailing spaces, tabs and newlines from a string, use the str.strip() method.

main.py
my_str = ' hi there ' result = my_str.strip() print(result) # 👉️ 'hi there' result = my_str.lstrip() print(repr(result)) # 👉️ 'hi there ' result = my_str.rstrip() print(repr(result)) # 👉️ ' hi there'

The str.strip method returns a copy of the string with the leading and trailing whitespace removed.

There are also str.lstrip() and str.rstrip() methods which remove the leading or trailing whitespace characters from the string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.