deleting the first and the last character of a string

Sean 'Shaleh' Perry shalehperry at
Wed Sep 11 21:46:03 CEST 2002

On Wednesday 11 September 2002 12:25, Markus wrote:
> Sean 'Shaleh' Perry wrote:
> >> Is there anything which says delete the first or last
> >> character of a string? I checked the string module on the web, but I
> >> couldn't find anything.
> >>
> >>>> s = '|f|o|o|'
> >>>> s.strip('|')
> >>>
> >>> s[1:-1]
> 'f|o|o'
> -Markus

True, but I like that the strip() call handles the case where one of the 
delimiters is missing.  One of the bugs that always bit me in perl was using 
chop() to eat the newline character when there was not a newline there to 
eat.  I try to avoid putting myself in that situation now.

More information about the Python-list mailing list