Equivalent of Perl chomp?

Jeff Hinrichs jlh at cox.net
Thu Jan 31 18:36:07 EST 2002


Actually, it doesn't appear to the real "chomp" from perl.
> def chomp(s):
>   if s[-1:]=='\n': return s[:-1]
>   else: return s
According to the def of chomp: "...removes all trailing newlines from the
string"
http://www.perldoc.com/perl5.6/pod/func/chomp.html

So then wouldn't a more accurate representation of chomp be:
def chomp(s):
    while s[-1:]=='\n':
        s = s[:-1]
    return s

-Jeff
p.s. I'm a python newbie so if I made a bad string handling decision please
let me know.






More information about the Python-list mailing list