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

M

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.

Thanks in advance :)
 
S

Steve Holden

Mathias said:
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.

Thanks in advance :)

Modulo the error checking you should clearly do on the arguments:
.... return str(i)[pos-1]
....
regards
Steve
 
T

Thomas Guettler

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
 
S

Scott David Daniels

Thomas said:
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
Actually, this should be written:

mystring="12345"
try:
print "Found it at index %s" % mystring.index('3')
except ValueError:
print "not found"


--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top