[Tutor] finding digit in string

bob gailer bgailer at gmail.com
Mon Oct 8 20:06:23 CEST 2012


On 10/8/2012 12:43 PM, 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.

My way is:

import string
tt = string.maketrans('0123456789','0000000000')
ts = asdf3456'.translate(t) # yields 'asdf0000'
ts.find("0") # finds the next "0" which is in the same position as corresponding digit


-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list