2 min read

Python Remove Spaces from a String

Python Remove Spaces from a String
Photo by Nubelson Fernandes / Unsplash

How to remove all the spaces between strings?

If you want to remove all the spaces between each word in a sentence you can have something as the code below.

sentence = "Hello     world from       Python    .     "
result = ''.join(sentence.split())

result
'HelloworldfromPython.'

The split() built-in function will split the sentence into words. If you don't specify a separator the default one is any whitespace. Therefore, the result of running sentence.split() is as follows:

sentence.split()
['Hello', 'world', 'from', 'Python', '.']

then using join(), and passing as an argument an array of strings it joins each word using the characters defined initially. Since, there are no characters defined it will join the array of words without adding any characters.

An alternative solution for this would be to use another built-in function called replace() as follows:

sentence = "Hello     world from       Python    .     "
sentence.replace(" ", "")
'HelloworldfromPython.'

Here, we replace any whitespace with nothing :) . As a result all the whitespaces are removed.

How to remove extra spaces between strings?

If you want to just have one space between each word in a sentence you can have something as the code below.

sentence = "Hello     world from       Python    .     "
result = ' '.join(sentence.split())

result
'Hello world from Python .'

The split() built-in function will split the sentence into words. If you don't specify a separator the default one is any whitespace. Therefore, the result of running sentence.split() is as follows:

sentence.split()
['Hello', 'world', 'from', 'Python', '.']

then using join(), and passing as an argument an array of strings it joins each word using the space defined initially.

Note: The code above is considered idiomatic for Python. I believe is the easiest approach to remove some or all whitespaces from a string.

How to remove leading and trailing spaces in Python?

For this, the easiest way is to the built-in strip() function as follows:

sentence = "      Hello     world from       Python    .     "

sentence.strip()
'Hello     world from       Python    .'

Note: If you only want to remove leading spaces you can use lstrip(). Similarly, if you want to remove trailing whitespaces you can use rstrip()

Other ways to remove whitespaces from a string in Python

There are some other ways as well.

  1. Regular expressions. You can use a regular expression to remove whitespaces or create a more complex ways to manipulate a string.
  2. Using the built-in translate function.

I will not explain these ways since I believe are complex and make the code harder to understand and as a result harder to maintain. Avoid doing this!

Video

https://youtube.com/shorts/RM7ZwD8md6g

The end

Congratulations! You made it to the end. It was a simple one :)

If you like the content here please consider subscribing.

If you have any comments or feedback feel free to reach me @costapiy