[Tutor] position of an element in list:

Steven D'Aprano steve at pearwood.info
Fri Jul 23 17:43:01 CEST 2010


On Fri, 23 Jul 2010 11:22:54 pm Vineeth Rakesh wrote:
> Hello all,
>
> How to return the position of a character in a string. Say I have
> str1 = "welcome to the world" if i want to return the position of the
> first occurrence of "o" how to do it?

str1.find("o") will return the index of the first "o", or -1 if not 
found.

str1.rfind("o") does the same, but searches from the right instead of 
the left.

str1.index("o") is like find, but it raises an exception instead of 
returning -1. Naturally there is a rindex as well.



-- 
Steven D'Aprano


More information about the Tutor mailing list