[Tutor] position of an element in list:

2010-07-23 Thread Vineeth Rakesh
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?

Thanks
Vin
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] position of an element in list:

2010-07-23 Thread Luke Paireepinart
You can do it with any iterator Astr.index('o') I'm not sure what happens 
when there are multiple instances of 'o' though, check the docs on index.

Sent from my iPhone

On Jul 23, 2010, at 8:22 AM, Vineeth Rakesh vineethrak...@gmail.com 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?
 
 Thanks
 Vin 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] position of an element in list:

2010-07-23 Thread Christian Witts

On 23/07/2010 15:22, 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?


Thanks
Vin


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
   


Strings have a function called index, which take a string argument which 
is what you're looking for.  So you can do str1.index('o') which would 
return 4.


--
Kind Regards,
Christian Witts


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] position of an element in list:

2010-07-23 Thread Steven D'Aprano
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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] position of an element in list:

2010-07-23 Thread Mark Lawrence

On 23/07/2010 16:43, Steven D'Aprano wrote:

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.



For the OP and possibly others, all of these methods have optional start 
and end arguments.  Help for find shows:-


find(...)
S.find(sub [,start [,end]]) - int

Return the lowest index in S where substring sub is found,
such that sub is contained within s[start:end].  Optional
arguments start and end are interpreted as in slice notation.

Return -1 on failure.

Mark Lawrence


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] position of an element in list:

2010-07-23 Thread Alan Gauld


Vineeth Rakesh vineethrak...@gmail.com wrote

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?


Others have answered but don't forget Python's help() facility.


help(str)


Would have probably got your answer a lot faster than posting a
question and waiting for replies.

We don't mind helping but for these kinds of question its usually
quicker to try a help(), dir() or even a Google search first. It saves
your time and ours.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor