寫一個 Python 程序,通過一個實例,使用 islower 和 isupper 檢查字符是小寫還是大寫。
使用 islower 和 isupper 函數檢查字符是小寫還是大寫的 Python 程序
在這個 Python 示例中,我們使用 islower 和 isupper 字符串函數來檢查給定字符是小寫還是大寫。
# Python Program to check character is Lowercase or Uppercase
ch = input("Please Enter Your Own Character : ")
if(ch.isupper()):
print("The Given Character ", ch, "is an Uppercase Alphabet")
elif(ch.islower()):
print("The Given Character ", ch, "is a Lowercase Alphabet")
else:
print("The Given Character ", ch, "is Not a Lower or Uppercase Alphabet")
檢查字符是否小寫的 Python 程序
這個 python 程序允許用戶輸入任何字符。接下來,我們使用 Elif 語句來檢查用戶給定的字符是小寫還是大寫。
- 這裡 If 語句檢查字符是否大於等於小 a,小於等於 z,如果是 TRUE,則為大寫。否則,它進入 elif 語句。
- 在 Elif 中,我們檢查給定的字符是否大於或等於 A,小於或等於 z。如果為真,則它是小寫字符。否則,它不是小寫字母或大寫字母。
# Python Program to check character is Lowercase or Uppercase
ch = input("Please Enter Your Own Character : ")
if(ch >= 'A' and ch <= 'Z'):
print("The Given Character ", ch, "is an Uppercase Alphabet")
elif(ch >= 'a' and ch <= 'z'):
print("The Given Character ", ch, "is a Lowercase Alphabet")
else:
print("The Given Character ", ch, "is Not a Lower or Uppercase Alphabet")
Python 字符是小寫還是大寫輸出
Please Enter Your Own Character : #
The Given Character # is Not a Lower or Uppercase Alphabet
>>>
Please Enter Your Own Character : T
The Given Character T is an Uppercase Alphabet
>>>
Please Enter Your Own Character : g
The Given Character g is a Lowercase Alphabet
Python 程序使用 ASCII 值檢查字符是否為小寫
在這段 Python 代碼中,我們使用 ASCII 值來檢查字符是大寫還是小寫。
ch = input("Please Enter Your Own Character : ")
if(ord(ch) >= 65 and ord(ch) <= 90):
print("The Given Character ", ch, "is an Uppercase Alphabet")
elif(ord(ch) >= 97 and ord(ch) <= 122):
print("The Given Character ", ch, "is a Lowercase Alphabet")
else:
print("The Given Character ", ch, "is Not a Lower or Uppercase Alphabet")
Please Enter Your Own Character : o
The Given Character o is a Lowercase Alphabet
>>>
Please Enter Your Own Character : R
The Given Character R is an Uppercase Alphabet
>>>
Please Enter Your Own Character : $
The Given Character $ is Not a Lower or Uppercase Alphabet
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/306416.html