Substitute Multiple Whitespace with Single Whitespace in Python

Substitute multiple whitespace with single whitespace in Python

A simple possibility (if you'd rather avoid REs) is

' '.join(mystring.split())

The split and join perform the task you're explicitly asking about -- plus, they also do the extra one that you don't talk about but is seen in your example, removing trailing spaces;-).

How to best replace multiple whitespaces by one (in python)?

You can use:

import re
a = re.sub(' +',' ',a)

Is there a simple way to remove multiple spaces in a string?

>>> import re
>>> re.sub(' +', ' ', 'The quick brown fox')
'The quick brown fox'

replace multiple whitespace with single space but keep new lines in regex match (python)

You can use code below:

import re
test = "This is a test. \n Blah blah."
print(re.sub(r"(?:(?!\n)\s)+", " ",test))


This is a test. 
Blah blah.

Replace multi-spacing in strings with single whitespace - Python

look at this

In [1]: str1 = "This is    a  foo bar   sentence with  crazy spaces that  irritates   my program "

In [2]: ' '.join(str1.split())
Out[2]: 'This is a foo bar sentence with crazy spaces that irritates my program'

The method split() returns a list of all the words in the string, using str as the separator (splits on all whitespace if left unspecified)

replace a single whitespace without replacing multiple whitespaces (Python)

use regex and word boundary:

>>> s="Lorem/ipsum/dolor/sit         amet consetetur"
>>> import re
>>> re.sub(r"\b \b","",s)
'Lorem/ipsum/dolor/sit ametconsetetur'

This technique also handles the more general case:

>>> s="Lorem/ipsum/dolor/sit         amet consetetur      adipisci velit"
>>> re.sub(r"\b \b","",s)
'Lorem/ipsum/dolor/sit ametconsetetur adipiscivelit'

for start & end spaces, you'll have to work slightly harder, but it's still doable:

>>> s=" Lorem/ipsum/dolor/sit         amet consetetur      adipisci velit "
>>> re.sub(r"(^|\b) (\b|$)","",s)
'Lorem/ipsum/dolor/sit ametconsetetur adipiscivelit'

Just for fun, a last variant: use re.split with a multiple space separation, preserve the split char using a group, then join the strings again, removing the spaces only if the string has some non-space in it:

"".join([x if x.isspace() else x.replace(" ","") for x in re.split("( {2,})",s)])

(I suppose that this is slower because of list creation & join though)

How do I replace multiple spaces with just one character?

This pattern will replace any groups of whitespace with a single underscore

newstring = '_'.join(input1.split())

If you only want to replace spaces (not tab/newline/linefeed etc.) it's probably easier to use a regex

import re
newstring = re.sub(' +', '_', input1)

How do I replace multiple blank spaces occuring in a text file with a single space in python

Split string by default splits string at spaces and ignores multiple consecutive spaces:

with open("test.234.txt", 'r') as f:

with open("test234(double space replaced by singleones)" ,"w+") as f:
for i in range(len(a)-1):
f.write(' '.join(a[i].split()))

Replace any number of white spaces with a single white space

You cannot use a regular expression in the replace() method for strings, you have to use the re module:

import re
mystring = re.sub(r'\s+', ' ', mystring)

Note the r prefix before the string literal, this makes sure that the backslash in your regular expressions is interpreted properly. It wouldn't actually make a difference here, but for different escape sequences it can cause serious problems. For example '\b' is a backspace character but r'\b' is a backslash followed by a 'b', which is used for matching word boundaries in regex.

Related Topics

Leave a reply