0

I want to use validation for the variable 'name' to make sure that the user is typing in words.

My ineffective solution:

name = input("What is your name").lower()
if name[0] == "a" or "b" or "c" or "d" or "e" #etc..:
    print("Success")
else:
    print("You must type in letters!")
roganjosh
  • 12,594
  • 4
  • 29
  • 46

2 Answers2

2

You may be looking for str.isalpha(), which returns true iff all the characters in a string are alphabetic.

(As an aside, to find out whether an object is a string is done with isinstance(x, str).)

>>> 'x'.isalpha()
True
>>> '6'.isalpha()
False
>>> '-'.isalpha()
False
>>> 'hello'.isalpha()
True
AKX
  • 152,115
  • 15
  • 115
  • 172
0

Try str.isalpha()

Replace str with the user input. Returns true if entered value is a string otherwise false.

devm
  • 227
  • 1
  • 12