Skip to content Skip to sidebar Skip to footer

Replacing All Multispaces With Single Spaces

For example s = 'a b c d e f ' Needs to be reduced to s = 'a b c d e f ' Right now I do something like this for i in xrange(arbitrarilyHighN

Solution 1:

Since the regular expression answer has already been given. You could also do it with iterative replacements.

while s.find("  ") is not -1:
    s = s.replace("  ", " ")

My original answer of splitting and rejoining gets rid of the leading and trailing whitespaces

' '.join(s.split())

Post a Comment for "Replacing All Multispaces With Single Spaces"