How to strip all whitespace from string

Question:

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'
Asked By: wrongusername

||

Answers:

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.

Answered By: Matthew Iselin

The simplest is to use replace:

"foo bart".replace(" ", "").replace("t", "")

Alternatively, use a regular expression:

import re
re.sub(r"s", "", "foo bart")
Answered By: carl

For Python 3:

>>> import re
>>> re.sub(r's+', '', 'strip my ntr ASCII and u00A0 u2003 Unicode spaces')
'stripmyASCIIandUnicodespaces'
>>> # Or, depending on the situation:
>>> re.sub(r'(s|u180B|u200B|u200C|u200D|u2060|uFEFF)+', '', 
... 'uFEFFttt 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:

Answered By: Tim Yates

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(" ", "")
'tfoonbar'

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.

Answered By: Roger Pate

Alternatively,

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

And here is Python3 version:

"strip my spaces".translate(str.maketrans('', '', string.whitespace))
Answered By: Dan Menes
import re
re.sub(' ','','strip my spaces')
Answered By: PrabhuPrakash

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

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(" ", ""))
Answered By: JohnSmitoff

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 (‘ tnrx0bx0c’) 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

Answered By: R. Arctor

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
Answered By: jferard

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'
Answered By: nBurn
  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()
Answered By: aleveha

Here’s another way using plain old list comprehension:

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

Example:

>>> aStr = 'aaanbbbtttccc  '
>>> print(aString)
aaa
bbb         ccc

>>> ''.join([c for c in aString if c not in [' ','t','n']])
'aaabbbccc'
Answered By: NYCeyes

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)
Answered By: ansh grover
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.