Assuming
s = 'a b c d e'
then
' '.join(s.split())
'a b c d e'
will give you the specified output.
This works by using split() to break the string into into a list of individual characters ['a', 'b', 'c', 'd', 'e']
and then joining them again with a single space in-between using the join() function into a string. The split()
also takes care of any leading or trailing blanks.
Based on Simple is better than complex (Zen of Python) in order to avoid the regex "two problem" problem :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With