创新互联Python教程:
专注于为中小企业提供网站设计制作、做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业泾县免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
用一个实例编写一个 Python 程序来检查字符是字母数字还是特殊字符。
这个 python 程序允许用户输入任何字符。接下来,我们使用 Elif 语句来检查用户给定的字符是字母、数字还是特殊字符。
# 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 字符是字母、数字或特殊字符输出
在这个 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 代码中,我们使用名为 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
本文名称:Python程序:检查字符是字母数字还是特殊字符
网页地址:http://www.mswzjz.cn/qtweb/news19/213369.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能