236

How do I strip all the spaces in a python string? For example, I want a string like strip my spaces to be turned into stripmyspaces, but I cannot seem to accomplish that with strip():

>>> 'strip my spaces'.strip()
'strip my spaces'
wrongusername
  • 18,564
  • 40
  • 130
  • 214

14 Answers14

398

Taking advantage of str.split's behavior with no sep parameter:

>>> s = " \t foo \n bar "
>>> "".join(s.split())
'foobar'

If you just want to remove spaces instead of all whitespace:

>>> s.replace(" ", "")
'\tfoo\nbar'

Premature optimization

Even though efficiency isn't the primary goal—writing clear code is—here are some initial timings:

$ python -m timeit '"".join(" \t foo \n bar ".split())'
1000000 loops, best of 3: 1.38 usec per loop
$ python -m timeit -s 'import re' 're.sub(r"\s+", "", " \t foo \n bar ")'
100000 loops, best of 3: 15.6 usec per loop

Note the regex is cached, so it's not as slow as you'd imagine. Compiling it beforehand helps some, but would only matter in practice if you call this many times:

$ python -m timeit -s 'import re; e = re.compile(r"\s+")' 'e.sub("", " \t foo \n bar ")'
100000 loops, best of 3: 7.76 usec per loop

Even though re.sub is 11.3x slower, remember your bottlenecks are assuredly elsewhere. Most programs would not notice the difference between any of these 3 choices.

  • It's probably slower than `\s+` substitution. I'd stick with re. – OTZ Sep 18 '10 at 01:04
  • 2
    @OTZ: You might be surprised, but see the "remember" note. –  Sep 18 '10 at 01:17
  • @Roger Hmm. interesting. Have you tried the `s.translate` method by any chance? It probably beats all the methods shown on this page. – OTZ Sep 18 '10 at 01:21
  • @Roger Pate: You don't need the 'table' argument for translate, it can be `None` -- although, surprisingly, that makes it slower... – martineau Sep 18 '10 at 19:31
  • One other thing. Passing " " as the second argument to translate doesn't strip out all forms of whitespace. – martineau Sep 18 '10 at 19:35
  • @martineau: Thanks; help(str.translate) lied to me! (It says table must be a string of length 256.) I'll just remove that section; fixing the various issues isn't worth it. (Anyone curious: check revision history.) –  Sep 18 '10 at 19:38
  • 1
    Try `myString.translate(None, " \t\r\n\v")`. It only takes 83% as long as Roger's fastest (split and join) technique. Not sure if it covers all the white space characters that split does, but it will probably suffice for most ASCII applications. – brianmearns Mar 15 '12 at 18:47
  • This does not work with all strings. Try running this on ```"""http://myserver.com/api/interpreter?data=[out:json][timeout:25]; ( way["building"="house"](40,-74,40,-74); ); out body; >; out skel qt;"""``` EDIT comment does not seem to support whitespace =( – Tommy Mar 27 '18 at 14:32
  • 1
    I did not know that .split() will catch any kind of whitespace and I bet the next person reading my code won't know that either (this includes my future self six months from now). – Tristan Feb 13 '19 at 16:18
85

For Python 3:

>>> import re
>>> re.sub(r'\s+', '', 'strip my \n\t\r ASCII and \u00A0 \u2003 Unicode spaces')
'stripmyASCIIandUnicodespaces'
>>> # Or, depending on the situation:
>>> re.sub(r'(\s|\u180B|\u200B|\u200C|\u200D|\u2060|\uFEFF)+', '', \
... '\uFEFF\t\t\t strip all \u000A kinds of \u200B whitespace \n')
'stripallkindsofwhitespace'

...handles any whitespace characters that you're not thinking of - and believe us, there are plenty.

\s on its own always covers the ASCII whitespace:

  • (regular) space
  • tab
  • new line (\n)
  • carriage return (\r)
  • form feed
  • vertical tab

Additionally:

  • for Python 2 with re.UNICODE enabled,
  • for Python 3 without any extra actions,

...\s also covers the Unicode whitespace characters, for example:

  • non-breaking space,
  • em space,
  • ideographic space,

...etc. See the full list here, under "Unicode characters with White_Space property".

However \s DOES NOT cover characters not classified as whitespace, which are de facto whitespace, such as among others:

  • zero-width joiner,
  • Mongolian vowel separator,
  • zero-width non-breaking space (a.k.a. byte order mark),

...etc. See the full list here, under "Related Unicode characters without White_Space property".

So these 6 characters are covered by the list in the second regex, \u180B|\u200B|\u200C|\u200D|\u2060|\uFEFF.

Sources:

Greg Dubicki
  • 5,983
  • 3
  • 55
  • 68
Tim Yates
  • 5,151
  • 2
  • 29
  • 29
  • 6
    This is a lot less hacky of a solution then the accepted answer. – Giga Chad Coding Jan 31 '18 at 20:10
  • 2
    This is more explicit than the other answers, so it takes the cake for me. – Tristan Feb 13 '19 at 16:18
  • 1
    The accepted answer is old, from the times where almost noone used Python 3 and therefore does not cover Unicode strings. It also unnecessarily go into optimization, which was not asked for. That's why I updated this answer as the best, in my opinion. – Greg Dubicki Sep 02 '20 at 10:00
  • 1
    @GregDubicki thanks for the additions. I added back the simple option since the full list might be overkill in some situations, or harmful if you need to keep a BOM (but hopefully you don't). Also noted the MVS since it is my favorite Unicode character, and was still Zs back when I originally wrote this. :P – Tim Yates Sep 02 '20 at 13:01
  • Thanks @TimYates! I made one more change to completely split the ASCII-only case from the UTF-8 one. This answer got long now, but more readable and easier to quickly copy-paste, as everyone likes that. ;) – Greg Dubicki Sep 03 '20 at 15:12
  • 1
    That edit seemed misleading (`\s` is enough in many situations with Unicode strings, not just ASCII) and added complexity by stressing Python 2 (which is EOL now, and the question is tagged Python 3). I think the brief mention of Python 2 and the explanation about the differences is enough info to choose the right approach. – Tim Yates Sep 03 '20 at 17:20
  • Ok, I am good with that @TimYates . :) I just made an almost empty edit because I think I deserve to be visible as an Editor of this answer. – Greg Dubicki Sep 04 '20 at 17:10
42

Alternatively,

"strip my spaces".translate( None, string.whitespace )

And here is Python3 version:

"strip my spaces".translate(str.maketrans('', '', string.whitespace))
Mirek Długosz
  • 4,205
  • 3
  • 24
  • 41
Dan Menes
  • 6,667
  • 1
  • 33
  • 35
18

Remove the Starting Spaces in Python

string1 = "    This is Test String to strip leading space"
print(string1)
print(string1.lstrip())

Remove the Trailing or End Spaces in Python

string2 = "This is Test String to strip trailing space     "
print(string2)
print(string2.rstrip())

Remove the whiteSpaces from Beginning and end of the string in Python

string3 = "    This is Test String to strip leading and trailing space      "
print(string3)
print(string3.strip())

Remove all the spaces in python

string4 = "   This is Test String to test all the spaces        "
print(string4)
print(string4.replace(" ", ""))
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
JohnSmitoff
  • 181
  • 1
  • 2
17

The simplest is to use replace:

"foo bar\t".replace(" ", "").replace("\t", "")

Alternatively, use a regular expression:

import re
re.sub(r"\s", "", "foo bar\t")
carl
  • 49,756
  • 17
  • 74
  • 82
4

As mentioned by Roger Pate following code worked for me:

s = " \t foo \n bar "
"".join(s.split())
'foobar'

I am using Jupyter Notebook to run following code:

i=0
ProductList=[]
while i < len(new_list): 
   temp=''                            # new_list[i]=temp=' Plain   Utthapam  '
   #temp=new_list[i].strip()          #if we want o/p as: 'Plain Utthapam'
   temp="".join(new_list[i].split())  #o/p: 'PlainUtthapam' 
   temp=temp.upper()                  #o/p:'PLAINUTTHAPAM' 
   ProductList.append(temp)
   i=i+2
Yogesh Awdhut Gadade
  • 2,498
  • 24
  • 19
3

Try a regex with re.sub. You can search for all whitespace and replace with an empty string.

\s in your pattern will match whitespace characters - and not just a space (tabs, newlines, etc). You can read more about it in the manual.

Matthew Iselin
  • 10,400
  • 4
  • 51
  • 62
3
import re
re.sub(' ','','strip my spaces')
PrabhuPrakash
  • 261
  • 2
  • 7
  • 4
    Welcome to SO. Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should vote it up once you have enough reputation – Maximilian Peters Oct 25 '16 at 07:37
  • 1
    This doesn't answer the question "how to remove all white space". It only remove spaces – Nic Sep 30 '18 at 20:38
3

The standard techniques to filter a list apply, although they are not as efficient as the split/join or translate methods.

We need a set of whitespaces:

>>> import string
>>> ws = set(string.whitespace)

The filter builtin:

>>> "".join(filter(lambda c: c not in ws, "strip my spaces"))
'stripmyspaces'

A list comprehension (yes, use the brackets: see benchmark below):

>>> import string
>>> "".join([c for c in "strip my spaces" if c not in ws])
'stripmyspaces'

A fold:

>>> import functools
>>> "".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))
'stripmyspaces'

Benchmark:

>>> from timeit import timeit
>>> timeit('"".join("strip my spaces".split())')
0.17734256500003198
>>> timeit('"strip my spaces".translate(ws_dict)', 'import string; ws_dict = {ord(ws):None for ws in string.whitespace}')
0.457635745999994
>>> timeit('re.sub(r"\s+", "", "strip my spaces")', 'import re')
1.017787621000025

>>> SETUP = 'import string, operator, functools, itertools; ws = set(string.whitespace)'
>>> timeit('"".join([c for c in "strip my spaces" if c not in ws])', SETUP)
0.6484303600000203
>>> timeit('"".join(c for c in "strip my spaces" if c not in ws)', SETUP)
0.950212219999969
>>> timeit('"".join(filter(lambda c: c not in ws, "strip my spaces"))', SETUP)
1.3164566040000523
>>> timeit('"".join(functools.reduce(lambda acc, c: acc if c in ws else acc+c, "strip my spaces"))', SETUP)
1.6947649049999995
jferard
  • 7,835
  • 2
  • 22
  • 35
2
  1. Parce your string to separate words
  2. Strip white spaces on both sides
  3. Join them with single space in the end

Final line of code:

' '.join(word.strip() for word in message_text.split()
aleveha
  • 21
  • 1
1

TL/DR

This solution was tested using Python 3.6

To strip all spaces from a string in Python3 you can use the following function:

def remove_spaces(in_string: str):
    return in_string.translate(str.maketrans({' ': ''})

To remove any whitespace characters (' \t\n\r\x0b\x0c') you can use the following function:

import string
def remove_whitespace(in_string: str):
    return in_string.translate(str.maketrans(dict.fromkeys(string.whitespace)))

Explanation

Python's str.translate method is a built-in class method of str, it takes a table and returns a copy of the string with each character mapped through the passed translation table. Full documentation for str.translate

To create the translation table str.maketrans is used. This method is another built-in class method of str. Here we use it with only one parameter, in this case a dictionary, where the keys are the characters to be replaced mapped to values with the characters replacement value. It returns a translation table for use with str.translate. Full documentation for str.maketrans

The string module in python contains some common string operations and constants. string.whitespace is a constant which returns a string containing all ASCII characters that are considered whitespace. This includes the characters space, tab, linefeed, return, formfeed, and vertical tab. Full documentation for string.whitespace

In the second function dict.fromkeys is used to create a dictionary where the keys are the characters in the string returned by string.whitespace each with value None. Full documentation for dict.fromkeys

R. Arctor
  • 718
  • 4
  • 15
1

If optimal performance is not a requirement and you just want something dead simple, you can define a basic function to test each character using the string class's built in "isspace" method:

def remove_space(input_string):
    no_white_space = ''
    for c in input_string:
        if not c.isspace():
            no_white_space += c
    return no_white_space

Building the no_white_space string this way will not have ideal performance, but the solution is easy to understand.

>>> remove_space('strip my spaces')
'stripmyspaces'

If you don't want to define a function, you can convert this into something vaguely similar with list comprehension. Borrowing from the top answer's join solution:

>>> "".join([c for c in "strip my spaces" if not c.isspace()])
'stripmyspaces'
nBurn
  • 25
  • 5
  • Curious: what about this is not 'optimal performance'? Specifically, your first example, the remove_space() function. – Hawkeye Parker Aug 31 '22 at 00:48
  • 1
    I'm not sure what the current state of Python's string handling is, but in the past string concatenation (foo_str+=bar_str) in Python used to have not so great performance. For light usage my example would be fine, but for processing large strings performance could become an issue. – nBurn Dec 03 '22 at 23:52
0

Here's another way using plain old list comprehension:

''.join([c for c in aString if c not in [' ','\t','\n']])

Example:

>>> aStr = 'aaa\nbbb\t\t\tccc  '
>>> print(aString)
aaa
bbb         ccc

>>> ''.join([c for c in aString if c not in [' ','\t','\n']])
'aaabbbccc'
NYCeyes
  • 5,215
  • 6
  • 57
  • 64
0

This got asked in an interview. So if you have to give a solution just by using strip method. Here's an approach -

s='string with spaces'
res=''.join((i.strip(' ') for i in s))
print(res)