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

superpollo utente at esempio.net
Tue May 11 11:13:50 EDT 2010


python at bdurham.com ha scritto:
> 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?
> 

could it be:

word[len(word)-1:] ?



More information about the Python-list mailing list