views:

151

answers:

4

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'
+1  A: 

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
I dunno how to use regexes :(
wrongusername
@wrongusername: Updated with a link to the re module manual page.
Matthew Iselin
+2  A: 

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
@carl `\s+` is better.
OTZ
+3  A: 
>>> import re
>>> re.sub(r'\s+', '', 'strip my spaces')
'stripmyspaces'

Also handles any whitespace characters that you're not thinking of (believe me, there are plenty).

Tim Yates
+9  A: 

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.

Roger Pate
+1. I like this one even better than my answer. Very pythonic.
Tim Yates
It's probably slower than `\s+` substitution. I'd stick with re.
OTZ
@OTZ: You might be surprised, but see the "remember" note.
Roger Pate
@Roger Hmm. interesting. Have you tried the `s.translate` method by any chance? It probably beats all the methods shown on this page.
OTZ
@Roger Pate: You don't need the 'table' argument for translate, it can be `None` -- although, surprisingly, that makes it slower...
martineau
One other thing. Passing " " as the second argument to translate doesn't strip out all forms of whitespace.
martineau
@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.)
Roger Pate