Python @ DjangoSpin

PyPro #67 Remove excessive spacing from a string

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Remove excessive spacing from a string in Python

Remove excessive spacing from a string in Python

Write a Regular Expression in Python to remove excessive spacing from a string.

Remove excessive spacing from a string

import re

sourceString = 'this is a     string      with excessive    spacing.'

print( re.sub('\s+', ' ', sourceString) )		# 'this is a string with excessive spacing.'

To learn more about Regular Expressions in Python, click here.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply