用一个实例编写一个 Python 程序来检查字符是字母数字还是特殊字符。
Python 程序检查字符是字母数字还是特殊字符
这个 python 程序允许用户输入任何字符。接下来,我们使用 Elif 语句来检查用户给定的字符是字母、数字还是特殊字符。
- 第一个 if 语句检查给定字符是在 A 和 z 之间还是 A 和 z 之间,如果为真,则为字母表。否则,它进入 elif 语句。
- 在 Elif 语句中,我们正在检查一个字符是否在 0 到 9 之间。如果为真,则为数字。否则,它是或特殊字符。
# Python Program to check character is Alphabet Digit or Special Character
ch = input("Please Enter Your Own Character : ")
if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')):
print("The Given Character ", ch, "is an Alphabet")
elif(ch >= '0' and ch <= '9'):
print("The Given Character ", ch, "is a Digit")
else:
print("The Given Character ", ch, "is a Special Character")
Python 字符是字母、数字或特殊字符输出
使用 ASCII 值验证字符是字母数字还是特殊字符的 Python 程序
在这个 Python 的例子中,我们使用 ASCII 值来检查字符是字母、数字还是特殊字符。
# Python Program to check character is Alphabet Digit or Special Character
ch = input("Please Enter Your Own Character : ")
if(ord(ch) >= 48 and ord(ch) <= 57):
print("The Given Character ", ch, "is a Digit")
elif((ord(ch) >= 65 and ord(ch) <= 90) or (ord(ch) >= 97 and ord(ch) <= 122)):
print("The Given Character ", ch, "is an Alphabet")
else:
print("The Given Character ", ch, "is a Special Character")
Python 字符是字母、数字或特殊字符输出
Please Enter Your Own Character : 0
The Given Character 0 is a Digit
>>>
Please Enter Your Own Character : r
The Given Character r is an Alphabet
>>>
Please Enter Your Own Character : @
The Given Character @ is a Special Character
Python 程序使用 isalpha,isdigit 函数查找字符是字母数字还是特殊字符
在这个 python 代码中,我们使用名为 isdigit 和 isalpha 的字符串函数来检查给定的字符是字母、数字还是特殊字符。
# Python Program to check character is Alphabet Digit or Special Character
ch = input("Please Enter Your Own Character : ")
if(ch.isdigit()):
print("The Given Character ", ch, "is a Digit")
elif(ch.isalpha()):
print("The Given Character ", ch, "is an Alphabet")
else:
print("The Given Character ", ch, "is a Special Character")
Python 字符是字母、数字或特殊字符输出
Please Enter Your Own Character : 4
The Given Character 4 is a Digit
>>>
Please Enter Your Own Character : &
The Given Character & is a Special Character
>>>
Please Enter Your Own Character : b
The Given Character b is an Alphabet