Python how to check if input is a letter or character

前端 未结 4 594
暖寄归人
暖寄归人 2021-01-26 10:53

How can I check if input is a letter or character in Python?

Input should be amount of numbers user wants to check. Then program should check if input given by user be

4条回答
  •  盖世英雄少女心
    2021-01-26 11:52

    You can use num.isnumeric() function that will return You "True" if input is number and "False" if input is not number.

    >>> x = raw_input()
    12345
    >>> x.isdigit()
    True
    

    You can also use try/catch:

    try:
       val = int(num)
    except ValueError:
       print("Not an int!")
    

提交回复
热议问题