str_whith_space = """
example line 1
example line 2
example line 3
example line 4"""
new_str = '\n'.join(el.strip() for el in str_whith_space.split('\n') if el.strip())
print(new_str)
"""
example line 1
example line 2
example line 3
example line 4
"""