Open this example in the interactive library
Source code
text = 'first line\nsecond line'
print('\n'.join(' '.join(line.split()) for line in text.splitlines()))Sample output
first line second line

Remove duplicate spaces without changing line breaks in Python.
Open this example in the interactive library
text = 'first line\nsecond line'
print('\n'.join(' '.join(line.split()) for line in text.splitlines()))first line second line