Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)
Jerry Hill
malaclypse2 at gmail.com
Tue May 11 11:32:22 EDT 2010
On Tue, May 11, 2010 at 10:37 AM, <python at 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
More information about the Python-list
mailing list