How to get rid the new line

Peter Hansen peter at engcorp.com
Fri Jun 28 01:52:21 CEST 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


-Peter



More information about the Python-list mailing list