Nov-14-2019, 07:37 PM
@bobfat that's different from ichabod's suggestion because ichabod's will short-circuit. You see how you complete the whole loop even if the first character is a digit? What you can do instead is to just return True within your if, and return False if the loop exits without the inner return.
Also, this looks like homework, so even though the OP says it isn't, I'd recommend not providing a full function to do what they're looking for, unless it's trivial...
And if the OP isn't doing this as homework, and they can use whatever module they want, they can have a one-liner:
Also, this looks like homework, so even though the OP says it isn't, I'd recommend not providing a full function to do what they're looking for, unless it's trivial...
And if the OP isn't doing this as homework, and they can use whatever module they want, they can have a one-liner:
import re def contains_digit(string): return re.match(r'.*\d', string) is not NoneIn use:
Output:>>> contains_digit('abc 123')
True
>>> contains_digit('abc one two three')
False