perl chomp equivalent in python?

Hrvoje Niksic hniksic at
Thu Feb 10 09:25:34 CET 2000

François Pinard <pinard at> writes:

>   if line and line[-1] == '\n':
>       line = line[:-1]
> You may remove the test for line if you know that it is not empty.


  if line[-1:] == '\n':
      line = line[:-1]

That works with both empty and non-empty lines.

More information about the Python-list mailing list