>>> def hasNumbers(inputString):
... return any(char.isdigit() for char in inputString)
...
>>> hasNumbers("I own 1 dog")
True
>>> hasNumbers("I own no dog")
False
或者可以使用正则表达式,如下所示
>>> import re
>>> def hasNumbers(inputString):
... return bool(re.search(r'\d', inputString))
...
>>> hasNumbers("I own 1 dog")
True
>>> hasNumbers("I own no dog")
False
可以将^{} 函数与^{} 函数一起使用,如下所示
或者可以使用正则表达式,如下所示
使用
参考号:https://docs.python.org/2/library/stdtypes.html#str.isalpha
您可以使用^{} 和^{} 的组合:
如果字符串中存在数字,则函数将返回
True
,否则返回False
。演示:
相关问题 更多 >
编程相关推荐