你怎么检查python字符串是否只包含数字?
16lz
2021-01-22
How do you check whether a string contains only numbers?
如何检查字符串是否只包含数字?
I've given it a go here. I'd like to see the simplest way to accomplish this.
我已经在这里试了一下。我想看看实现这一目标的最简单方法。
import string
def main():
isbn = input("Enter your 10 digit ISBN number: ")
if len(isbn) == 10 and string.digits == True:
print ("Works")
else:
print("Error, 10 digit number was not inputted and/or letters were inputted.")
main()
if __name__ == "__main__":
main()
input("Press enter to exit: ")
6 个解决方案
#1
145
You'll want to use the isdigit
method on your str
object:
您将要在str对象上使用isdigit方法:
if len(isbn) == 10 and isbn.isdigit():
From the isdigit
documentation:
从isdigit文档:
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.
如果字符串中的所有字符都是数字且至少有一个字符,则返回true,否则返回false。
For 8-bit strings, this method is locale-dependent.
对于8位字符串,此方法取决于区域设置。
更多相关文章
- 在java中调用python方法
- Python中字典合并的实现方法
- Python 学习笔记【list的操作方法】
- 使用pip安装tensorflow 0.80,python 使用tensorflow 0.80遇到的问
- Python - 去除字符串首尾填充
- Python处理字符串
- python list range 字符串的截取 如 text[1:5]
- 在save方法中创建两个对象
- 基于Python的XSS测试工具XSStrike使用方法