如何在 Python 中检查字符串是否仅包含数字?
Python 具有内置函数 isdigit(),当字符串中的所有字符都是数字 (介于 0-9) 时,此函数返回 true
>>> 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP