[Tutor] finding a number with str.find

Steven D'Aprano steve at pearwood.info
Mon Oct 8 20:31:12 CEST 2012

On 09/10/12 03:33, Benjamin Fishbein wrote:
> Is there a way to find the next character that is a digit (0-9) in a
>string? It seems that the str.find method can only find one particular
>character, rather than any character from among many.


For more complicated searching needs, either use a proper parser to
parse the string as needed, or use a regular expression as a kind of

import re
re.search(r'\d', 'I 8 sandwiches').start()

# returns 2

Beware though: there is a saying about regular expressions, and in
particular about the sort of people who reach for a reg ex to solve
nearly every problem:

"Some people, when faced with a problem, think 'I know, I'll use a
regular expression'. Now they have two problems."


More information about the Tutor mailing list