Slice last char from string without raising exception on empty string (Re: Extract all words that begin with x)

python at bdurham.com python at bdurham.com
Tue May 11 10:37:01 EDT 2010


Terry,

>  ... word[0:1] does the same thing. All Python programmers should learn to use slicing to extract a char from a string that might be empty.

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?

Thanks,
Malcolm



More information about the Python-list mailing list