如何在Python中检查字符串是否只包含数字?
Python内置函数isdigit()可返回字符串中的所有字符是否位数字(0-9)。
>>> string='9764135408'
>>> string.isdigit()
True
>>> string='091-9764135408'
>>> string.isdigit()
False
您也可以使用正则表达式来检查字符串是否只包含数字。
>>> import re
>>> bool(re.match('^[0-9]+','9764135408'))
True
>>> bool(re.match('^[0-9]+','091-9764135408'))
False
更多Python相关文章,请阅读:Python 教程