[Tutor] finding digit in string

Mark Lawrence breamoreboy at yahoo.co.uk
Mon Oct 8 19:41:58 CEST 2012


On 08/10/2012 17:43, Benjamin Fishbein wrote:
> I figured out a solution for the question I asked on here.
> To find the next digit (0-9) in a string, I use:
> text.find("0" or "1" or "2", etc.......)
> But is there a more elegant way to do this? The way I found uses a lot of typing.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

How about something like:-

for ch in text:
     if ch.isdigit():
         doSomething(ch)

or:-

for ch in text:
     if '0' <= ch <= '9':
         doSomething(ch)


If you want to use the offset for any reason use enumerate:-

for i, ch in enumerate(text):
     etc.

-- 
Cheers.

Mark Lawrence.



More information about the Tutor mailing list