Aman Deep Aman Deep - 1 year ago 118
Python Question

Remove spaces before newlines

I need to remove all spaces before the newline character throughout a string.

string = """
this is a line \n
this is another \n


string = """
this is a line\n
this is another\n

Answer Source

You can split the string into lines, strip off all whitespaces on the right using rstrip, then add a new line at the end of each line:

''.join([line.rstrip()+'\n' for line in string.splitlines()])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download