How can I remove a trailing newline?

Try the method rstrip() (see doc Python 2 and Python 3) >>> ‘test string\n’.rstrip() ‘test string’ Python’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp. >>> ‘test string \n \r\n\n\r \n\n’.rstrip() ‘test string’ To strip only newlines: >>> ‘test string \n \r\n\n\r \n\n’.rstrip(‘\n’) ‘test … Read more

How to remove leading and trailing zeros in a string? Python

What about a basic your_string.strip(“0”) to remove both trailing and leading zeros ? If you’re only interested in removing trailing zeros, use .rstrip instead (and .lstrip for only the leading ones). More info in the doc. You could use some list comprehension to get the sequences you want like so: trailing_removed = [s.rstrip(“0”) for s … Read more

How do I remove a trailing newline?

Try the method rstrip() (see doc Python 2 and Python 3) >>> ‘test string\n’.rstrip() ‘test string’ Python’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp. >>> ‘test string \n \r\n\n\r \n\n’.rstrip() ‘test string’ To strip only newlines: >>> ‘test string \n \r\n\n\r \n\n’.rstrip(‘\n’) ‘test … Read more