How to get rid the new line

Peter Hansen peter at
Thu Jun 27 19:52:21 EDT 2002

"James T. Dennis" wrote:
>  Could try this:
>         def chomp(line):
>                 if line[-1]=='\n':
>                         line=line[:-1]
>                 return line

Just make sure you pass chomp() only lines that are not equal
to the empty string ''.

>>> line = ''
>>> line[-1]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: string index out of range


More information about the Python-list mailing list