help please
help please

Reputation: 115

AttributeError: 'str' object has no attribute isalpha()

I'm new to python and am having trouble with strings. I'm getting "AttributeError: 'str' object has no attribute" but am confused as to why. I've provided some of my code so any advice would be helpful!

#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")

Upvotes: 0

Views: 21935

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174836

It would be string.isdigit() not 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'
>>> 

Upvotes: 4

Related Questions