新聞中心
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)從2013年成立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元禪城做網(wǎng)站,已為上家服務(wù),為禪城各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18980820575
寫(xiě)一個(gè) Python 程序,用一個(gè)實(shí)例來(lái)檢查字符是字母還是數(shù)字。
Python 程序檢查字符是字母還是數(shù)字
這個(gè) python 程序允許用戶(hù)輸入任何字符。接下來(lái),我們使用 Elif 語(yǔ)句來(lái)檢查用戶(hù)給定的字符是字母還是數(shù)字。
- 這里 If 語(yǔ)句檢查字符是在 A 和 Z 之間還是在 A 和 Z 之間,如果是 TRUE,則是字母表。否則,它進(jìn)入 elif 語(yǔ)句。
- 在 Elif 語(yǔ)句中,我們正在檢查給定的字符是否在 0 到 9 之間。如果是真的,就是一個(gè)數(shù)字;否則,它不是數(shù)字或字母。
# Python Program to check character is Alphabet or Digit
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 Not an Alphabet or a Digit")Python 字符是字母或數(shù)字輸出
Please Enter Your Own Character : j
The Given Character j is an Alphabet
>>>
Please Enter Your Own Character : 6
The Given Character 6 is a Digit
>>>
Please Enter Your Own Character : .
The Given Character . is Not an Alphabet or a Digit使用 ASCII 值驗(yàn)證字符是字母還是數(shù)字的 Python 程序
在這個(gè) Python 代碼中,我們使用 ASCII 值來(lái)檢查字符是字母還是數(shù)字。
# Python Program to check character is Alphabet or Digit
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 Not an Alphabet or a Digit")Please Enter Your Own Character : q
The Given Character q is an Alphabet
>>>
Please Enter Your Own Character : ?
The Given Character ? is Not an Alphabet or a Digit
>>>
Please Enter Your Own Character : 6
The Given Character 6 is a DigitPython 程序使用 isalpha,isdigit 函數(shù)查找字符是字母還是數(shù)字
在這個(gè)例子 python 代碼中,我們使用名為 isdigit 和 isalpha 的字符串函數(shù)來(lái)檢查給定的字符是字母還是數(shù)字。
# Python Program to check character is Alphabet or Digit
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 Not an Alphabet or a Digit") 網(wǎng)站標(biāo)題:Python程序:檢查字符是字母還是數(shù)字
轉(zhuǎn)載來(lái)于:http://m.fisionsoft.com.cn/article/dhgchdo.html


咨詢(xún)
建站咨詢(xún)
