编写一个Python程序,通过实际示例检查字符是否为数字。
Python 程序检查字符是否为数字
此Python程序允许用户输入任何字符。接下来,我们使用If Else语句检查用户输入的字符是否为数字。在这里,If语句检查字符是否大于或等于0,并且小于或等于9。如果为TRUE,则该字符是数字。否则,它不是数字。
# Python Program to check character is Digit or not
ch = input("Please Enter Your Own Character : ")
if(ch >= '0' and ch <= '9'):
print("The Given Character ", ch, "is a Digit")
else:
print("The Given Character ", ch, "is Not a Digit")
Python 字符是数字还是非数字的输出
Please Enter Your Own Character : 1
The Given Character 1 is a Digit
>>>
Please Enter Your Own Character : i
The Given Character i is Not a Digit
Python 程序使用 ASCII 值查找字符是否为数字
在此 Python 示例中,我们使用 ASCII 值来检查字符是否为数字。
# Python Program to check character is Digit or not
ch = input("Please Enter Your Own Character : ")
if(ord(ch) >= 48 and ord(ch) <= 57):
print("The Given Character ", ch, "is a Digit")
else:
print("The Given Character ", ch, "is Not a Digit")
Please Enter Your Own Character : 7
The Given Character 7 is a Digit
>>>
Please Enter Your Own Character : @
The Given Character @ is Not a Digit
Python 程序使用 isdigit 函数验证字符是否为数字
在此示例 Python 代码中,我们在 If Else 语句中使用 isdigit 字符串函数来检查给定字符是否为数字。
# Python Program to check character is Digit or not
ch = input("Please Enter Your Own Character : ")
if(ch.isdigit()):
print("The Given Character ", ch, "is a Digit")
else:
print("The Given Character ", ch, "is Not a Digit")
