Remove all whitespace in a string
Asked Answered
C

15

1230

I want to eliminate all the whitespace from a string, on both ends, and in between words.

I have this Python code:

def my_handle(self):
    sentence = ' hello  apple  '
    sentence.strip()

But that only eliminates the whitespace on both sides of the string. How do I remove all whitespace?

Crosstie answered 25/11, 2011 at 13:51 Comment(3)
What should your result look like? hello apple? helloapple?Myrtamyrtaceous
@JoachimPileborg, not exactly I think, because it's also about reducung whitespace between the words.Filagree
Correct me if wrong, but "whitespace" is not synonymous with "space characters". The current answer marked as correct does not remove all whitespace. But, since it's marked as correct it must have answered the intended question? So we should edit the question to reflect the accepted answer? @Kalanamith Did, or do, you want to remove all whitespace or only spaces?Giuseppe
B
2331

If you want to remove leading and ending spaces, use str.strip():

>>> "  hello  apple  ".strip()
'hello  apple'

If you want to remove all space characters, use str.replace() (NB this only removes the “normal” ASCII space character ' ' U+0020 but not any other whitespace):

>>> "  hello  apple  ".replace(" ", "")
'helloapple'

If you want to remove duplicated spaces, use str.split() followed by str.join():

>>> " ".join("  hello  apple  ".split())
'hello apple'
Bisk answered 25/11, 2011 at 13:56 Comment(8)
The greatness of this function is that it also removes the '\r\n' from the html file I received from Beautiful Soup.Thermogenesis
I like "".join(sentence.split()), this removes all whitespace (spaces, tabs, newlines) from anywhere in sentence.Hysteric
begginner here. Can someone explain me why print(sentence.join(sentence.split())) results to 'hello hello appleapple'? Just want to understand how code is processed here.Forsberg
@YannisDran check the str.join() documentation, when you call sentence.join(str_list) you ask python to join items from str_list with sentenceas separator.Aftertime
"".join(sentence.split()) is indeed the canonical solution, efficiently removing all whitespace rather than merely spaces. Mark Byers' excellent answer should probably have been accepted in lieu of this less applicable answer.Labors
in case of more than 2 whitespaces in the end string, extra sentence.strip().rstrip() can help same goes for sentence.strip().lstrip() for beginnig. Not perfect, but works without the need for re that's in case simple .strip() misses themWaldrup
The solution works but is slightly confusing - when using replace, it returns the string, and doesn't replace in-place, so you need sentence = sentence.replace(" ", "")Pyrochemical
Agree with @CecilCurry If the string can be anything and the intention is to replace all whitespaces, it should be ` "".join(sentence.split()) `Hendeca
M
432

To remove only spaces use str.replace:

sentence = sentence.replace(' ', '')

To remove all whitespace characters (space, tab, newline, and so on) you can use split then join:

sentence = ''.join(sentence.split())

or a regular expression:

import re
pattern = re.compile(r'\s+')
sentence = re.sub(pattern, '', sentence)

If you want to only remove whitespace from the beginning and end you can use strip:

sentence = sentence.strip()

You can also use lstrip to remove whitespace only from the beginning of the string, and rstrip to remove whitespace from the end of the string.

Myrtamyrtaceous answered 25/11, 2011 at 13:54 Comment(2)
Note: You don't need to compile step, re.sub (and friends) cache the compiled pattern. See also, Emil's answer.Unstained
python3: yourstr.translate(str.maketrans('', '', ' \n\t\r'))Phyllotaxis
H
160

An alternative is to use regular expressions and match these strange white-space characters too. Here are some examples:

Remove ALL spaces in a string, even between words:

import re
sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)

Remove spaces in the BEGINNING of a string:

import re
sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)

Remove spaces in the END of a string:

import re
sentence = re.sub(r"\s+$", "", sentence, flags=re.UNICODE)

Remove spaces both in the BEGINNING and in the END of a string:

import re
sentence = re.sub("^\s+|\s+$", "", sentence, flags=re.UNICODE)

Remove ONLY DUPLICATE spaces:

import re
sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))

(All examples work in both Python 2 and Python 3)

Hintz answered 19/2, 2015 at 13:5 Comment(3)
Did not work for "\u202a1234\u202c". Gives the same output: u'\u202a1234\u202c'Nymphet
@Sarang: Those are not whitespace characters (google them and you'll see) but "General Punctuation". My answer only deals with removing characters classified as whitespace.Beitz
This is the only solution I see here that removes those damn pesky unicode whitespace characters, thanks famTempered
L
65

"Whitespace" includes space, tabs, and CRLF. So an elegant and one-liner string function we can use is str.translate:

Python 3

' hello  apple '.translate(str.maketrans('', '', ' \n\t\r'))

OR if you want to be thorough:

import string
' hello  apple'.translate(str.maketrans('', '', string.whitespace))

Python 2

' hello  apple'.translate(None, ' \n\t\r')

OR if you want to be thorough:

import string
' hello  apple'.translate(None, string.whitespace)
Lavonna answered 28/11, 2015 at 3:36 Comment(3)
This won't help with Unicode whitespace like \xc2\xa0Peppers
ans.translate( None, string.whitespace ) produces only builtins.TypeError: translate() takes exactly one argument (2 given) for me. Docs says that argument is a translate table, see string.maketrans(). But see comment by Amnon Harel, below.Intort
' hello apple'.translate(str.maketrans('', '', string.whitespace)) Note: its better to make a variable to store the trans-table if you intend to do this multiple times.Francinefrancis
F
20

For removing whitespace from beginning and end, use strip.

>> "  foo bar   ".strip()
"foo bar"
Filagree answered 25/11, 2011 at 13:56 Comment(2)
The question specifically asks for removing all of the whitespace and not just at the ends. Please take notice.Shreveport
This answer is irrelevant to this questionDonte
T
13
' hello  \n\tapple'.translate({ord(c):None for c in ' \n\t\r'})

MaK already pointed out the "translate" method above. And this variation works with Python 3 (see this Q&A).

Tacy answered 26/9, 2016 at 9:54 Comment(1)
Thanks! Or, xxx.translate( { ord(c) :None for c in string.whitespace } ) for thoroughness.Intort
A
11

In addition, strip has some variations:

Remove spaces in the BEGINNING and END of a string:

sentence= sentence.strip()

Remove spaces in the BEGINNING of a string:

sentence = sentence.lstrip()

Remove spaces in the END of a string:

sentence= sentence.rstrip()

All three string functions strip lstrip, and rstrip can take parameters of the string to strip, with the default being all white space. This can be helpful when you are working with something particular, for example, you could remove only spaces but not newlines:

" 1. Step 1\n".strip(" ")

Or you could remove extra commas when reading in a string list:

"1,2,3,".strip(",")
Aronoff answered 6/4, 2018 at 20:51 Comment(0)
B
7

Be careful:

strip does a rstrip and lstrip (removes leading and trailing spaces, tabs, returns and form feeds, but it does not remove them in the middle of the string).

If you only replace spaces and tabs you can end up with hidden CRLFs that appear to match what you are looking for, but are not the same.

Brucite answered 12/11, 2014 at 19:30 Comment(1)
Although this is a good point, this isn't really an answer and should be a comment unless you provide a solution. Would you care to provide a solution for this is exactly what I'm looking for? CheersCausative
R
6

eliminate all the whitespace from a string, on both ends, and in between words.

>>> import re
>>> re.sub("\s+", # one or more repetition of whitespace
    '', # replace with empty string (->remove)
    ''' hello
...    apple
... ''')
'helloapple'

Python docs:

Ruphina answered 13/3, 2020 at 15:51 Comment(1)
I know re has been suggested before, but I found that the actual answer to the question title was a bit hidden amongst all the other options.Ruphina
C
5

I use split() to ignore all whitespaces and use join() to concatenate strings.

sentence = ''.join(' hello  apple  '.split())
print(sentence) #=> 'helloapple'

I prefer this approach because it is only a expression (not a statement).
It is easy to use and it can use without binding to a variable.

print(''.join(' hello  apple  '.split())) # no need to binding to a variable
Cicada answered 29/7, 2021 at 14:33 Comment(0)
B
3
import re    
sentence = ' hello  apple'
re.sub(' ','',sentence) #helloworld (remove all spaces)
re.sub('  ',' ',sentence) #hello world (remove double spaces)
Bak answered 24/10, 2016 at 12:46 Comment(1)
the question was too remove all white space which includes tabs and new line characters, this snippet will only remove regular spaces.Rosado
T
3

In the following script we import the regular expression module which we use to substitute one space or more with a single space. This ensures that the inner extra spaces are removed. Then we use strip() function to remove leading and trailing spaces.

# Import regular expression module
import re

# Initialize string
a = "     foo      bar   "

# First replace any number of spaces with a single space
a = re.sub(' +', ' ', a)

# Then strip any leading and trailing spaces.
a = a.strip()

# Show results
print(a)
Triune answered 19/2, 2022 at 10:59 Comment(2)
It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code.Wideranging
@theTinMan thanks for the recommendation I just added the explanations.Triune
O
1

I found that this works the best for me:

test_string = '  test   a   s   test '
string_list = [s.strip() for s in str(test_string).split()]
final_string = ' '.join(string_array)
# final_string: 'test a s test'

It removes any whitespaces, tabs, etc.

Oligopsony answered 25/7, 2022 at 10:8 Comment(0)
O
1

All string characters are unicode literal in Python 3; as a consequence, since str.split() splits on all white space characters, that means it splits on unicode white space characters. So split + join syntax (as in 1, 2, 3) will produce the same output as re.sub with the UNICODE flag (as in 4); in fact, the UNICODE flag is redundant here (as in 2, 5, 6, 7).

import re
import sys

# all unicode characters
sentence = ''.join(map(chr, range(sys.maxunicode+1)))

# remove all white space characters
x = ''.join(sentence.split())
y = re.sub(r"\s+", "", sentence, flags=re.UNICODE)
z = re.sub(r"\s+", "", sentence)

x == y == z      # True

In terms of performance, since Python's string methods are optimized, they are much faster than regex. As the following timeit test shows, when removing all white space characters from the string in the OP, Python string methods are over 7 times faster than re option.

import timeit

import timeit

setup = """
import re
s = ' hello  \t apple  '
"""

t1 = min(timeit.repeat("''.join(s.split())", setup))
t2 = min(timeit.repeat("re.sub(r'\s+', '', s, flags=re.UNICODE)", setup))


t2 / t1  # 7.868004799367726
Olivares answered 31/8, 2023 at 4:46 Comment(0)
L
-2

try this.. instead of using re i think using split with strip is much better

def my_handle(self):
    sentence = ' hello  apple  '
    ' '.join(x.strip() for x in sentence.split())
#hello apple
    ''.join(x.strip() for x in sentence.split())
#helloapple
Liter answered 10/10, 2020 at 19:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.