用一個實際例子寫一個 Python 程序來檢查字符是否是字母。
Python 程序檢查字符是否是字母
這個 python 程序允許用戶輸入任何字符。接下來,我們使用 If Else 語句來檢查用戶給定的字符是否是字母表。這裡 If 語句檢查字符是在 A 和 z 之間還是在 A 和 z 之間,如果是 TRUE,則是字母表;否則,它就不是字母表。
# Python Program to check character is Alphabet or not
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")
else:
print("The Given Character ", ch, "is Not an Alphabet")
Python 字符是字母還是不輸出
Please Enter Your Own Character : q
The Given Character q is an Alphabet
>>>
Please Enter Your Own Character : 8
The Given Character 8 is Not an Alphabet
Python 程序使用 ASCII 值驗證字符是否是字母
在這個 Python 代碼中,我們使用 If Else 語句中的 ASCII 值來檢查字符是否是字母表。
# Python Program to check character is Alphabet or not
ch = input("Please Enter Your Own Character : ")
if((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")
Please Enter Your Own Character : W
The Given Character W is an Alphabet
>>>
Please Enter Your Own Character : #
The Given Character # is Not an Alphabet
尋找字符的 Python 程序是使用 isalpha 函數的字母表
在這個例子 python 代碼中,我們使用 isalpha 字符串函數來檢查給定的字符是否是字母表。
# Python Program to check character is Alphabet or not
ch = input("Please Enter Your Own Character : ")
if(ch.isalpha()):
print("The Given Character ", ch, "is an Alphabet")
else:
print("The Given Character ", ch, "is Not an Alphabet")
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/249573.html