Prev: Slice last char from string without raising exception on emptystring (Re: Extract all words that begin with x)
Next: documentation bug? (format spec mini language)
From: Jerry Hill on 11 May 2010 11:32 On Tue, May 11, 2010 at 10:37 AM, <python(a)bdurham.com> wrote: > Is there an equivalent way to slice the last char from a string (similar > to an .endswith) that doesn't raise an exception when a string is empty? If you use negative indexes in the slice, they refer to items from the end of the sequence instead of the front. So slicing the last character from the string would be: word[-1:] -- Jerry
From: python on 11 May 2010 12:01 Superpollo, > word[len(word)-1:] Perfect! Thank you, Malcolm
From: python on 11 May 2010 12:05 Jerry, > If you use negative indexes in the slice, they refer to items from the end of the sequence instead of the front. So slicing the last character from the string would be: > > word[-1:] Perfect! Thank you, Malcolm
From: James Mills on 11 May 2010 12:35 On Wed, May 12, 2010 at 2:01 AM, <python(a)bdurham.com> wrote: >> word[len(word)-1:] This works just as well: >>> word[-1:] cheers James
From: superpollo on 11 May 2010 12:39
James Mills ha scritto: > On Wed, May 12, 2010 at 2:01 AM, <python(a)bdurham.com> wrote: >>> word[len(word)-1:] > > This works just as well: > >>>> word[-1:] d'uh. ;-) |