[Newbie]: How to find a specified digit in a number?

Thomas Guettler guettli at thomas-guettler.de
Wed Nov 10 10:42:12 EST 2004


Am Wed, 10 Nov 2004 16:29:13 +0100 schrieb Mathias:

> Hi everyone.
> 
> I'm making a little script which finds the right number from a couple of
> choices, but I need a function or script which finds a specified digit,
> eg. FindDigit(3579, 3) = 7.

Hi,

The example looks like you need the third
characater of the string:
print "3579"[2] # zero based

you can use int() to make it an integer
and str() to make an integer a string.

Or you want to search in the string for e.g. "5":

mystring="12345"
idx=mystring.find("3")
if idx==-1:
    print "not found"
else:
    print "Found it at index %s" % idx


HTH,
 Thomas




More information about the Python-list mailing list