AttributeError:“str”对象没有属性isalpha()

2024-05-14 03:29:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,字符串有问题。我得到了“attributerror:”str“object没有属性”,但是我很困惑为什么。我已经提供了一些我的代码,所以任何建议都会有帮助!

#Have user enter their string.
string = input("Enter a string: ")
    #Find if string only contains letters and spaces
    if string.isalpha():
        print("Only alphabetic letters and spaces: yes")
    else:
        print("Only alphabetic letters and spaces: no")

    #Find if string is only numeric.
    if string.isdigits():
        print("Only numeric digits: yes")
    else:
        print("Only numeric digits: no")

Tags: andnoonlystringiffindelseyes
1条回答
网友
1楼 · 发布于 2024-05-14 03:29:29

应该是string.isdigit()而不是string.isdigits()

>>> '9'.isdigit()
True
>>> '9'.isdigits()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    '9'.isdigits()
AttributeError: 'str' object has no attribute 'isdigits'
>>> 

相关问题 更多 >