For example:

'I am good boy' 

How do I do this:

 'I am good boy' 

    2 answers 2

    Hurry to accept answers

     ' '.join(i for i in 'I am good boy'.split(' ') if i != '') 
    • 3
      It can be simplified to replace arbitrary whitespace characters (including '\ n') with one space: s = ' '.join(s.split()) - jfs

    Using regular expressions:

     In [1]: s = 'I am good boy' In [2]: import re In [3]: re.sub(r'\s+', ' ', s) Out[3]: 'I am good boy'