Skip to content Skip to sidebar Skip to footer

Remove Unwanted Space In Between A String

I wanna know how to remove unwanted space in between a string. For example: >>> a = 'Hello world' and i want to print it removing the extra middle spaces. Hello worl

Solution 1:

This will work:

" ".join(a.split())

Without any arguments, a.split() will automatically split on whitespace and discard duplicates, the " ".join() joins the resulting list into one string.

Solution 2:

Regular expressions also work

>>>import re>>>re.sub(r'\s+', ' ', 'Hello     World')
'Hello World'

Post a Comment for "Remove Unwanted Space In Between A String"