From: Jerry Hill on
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
Superpollo,

> word[len(word)-1:]

Perfect! Thank you,

Malcolm
From: python on
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
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
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. ;-)