Coder Perfect

In a string, remove all whitespace.

Problem

I want to remove all whitespace from a string, both at the beginning and conclusion, and in between words.

This is the Python code I have:

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

However, this merely removes whitespace on both sides of the string. How can I get rid of all the whitespace?

Asked by co2f2e

Solution #1

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

sentence = ' hello  apple'
sentence.strip()
>>> 'hello  apple'

Use str.replace() to get rid of all space characters:

(Note that this simply eliminates the “standard” ASCII space character” U+0020, but no other whitespace.)

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

Str.split() can be used to remove duplicated spaces:

sentence = ' hello  apple'
" ".join(sentence.split())
>>> 'hello apple'

Answered by Cédric Julien

Solution #2

Use str.replace to remove only spaces:

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

You can use split then join to eliminate all whitespace characters (space, tab, newline, and so on):

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

Alternatively, you can use a regular expression:

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

You can use strip: to eliminate whitespace solely from the beginning and end of a line.

sentence = sentence.strip()

You may also use lstrip to remove whitespace from the beginning of a string and rstrip to remove whitespace from the end.

Answered by Mark Byers

Solution #3

Regular expressions can also be used to match these unusual white-space letters. Some instances are as follows:

In a string, remove ALL spaces, even between words:

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

Remove spaces from a string’s BEGINNING:

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

Remove spaces from a string’s END:

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

Remove spaces from both the start and end of a string:

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

ONLY DUPLICATE spaces should be removed:

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

(All examples are Python 2 and Python 3 compatible.)

Answered by Emil Stenström

Solution #4

Space, tabs, and CRLF are all examples of “whitespace.” str.translate is a simple and beautiful string function that we can use:

Python 3

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

Alternatively, if you want to be thorough:

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

Python 2

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

Alternatively, if you want to be thorough:

import string
' hello  apple'.translate(None, string.whitespace)

Answered by MaK

Solution #5

Strip is a tool for removing whitespace from the beginning and end of a document.

>> "  foo bar   ".strip()
"foo bar"

Answered by wal-o-mat

Post is based on https://stackoverflow.com/questions/8270092/remove-all-whitespace-in-a-string