Python中的 isupper() 函數的作用是檢查一個字符串中的字符是否都為大寫形式,如果字符串不為空,且所有字符都為大寫形式的話就返回 True ,否則返回 False 。
可以認為,Python 的 isupper() 函數的作用與 islower() 函數是相反的。后者的作用是檢查一個字符串的所有字符是否都為小寫形式。
str_name.isupper()
str_name是要檢查的字符串或字符串變量;
該函數沒有參數;
該函數的返回值是邏輯值:True 或 False.
1、只包含字母且所有字符都為大寫
str1 = "WELCOME TO SHANGHAI"
print(str1.isupper())
str1 = "ΓΔΘΚ" #希臘大寫字母
print(str1.isupper())
str1 = "БДЁЖ" #俄文字母
print(str1.isupper())
在Python3.8.2中的執行情況如下圖所示:
2、只包含大小寫字母
str1 = "Welcome to Hebei"
print(str1.isupper())
str1 = "ΦσΣ"
print(str1.isupper())
輸出:
False
False
3、字母與非字母混排
str1 = "武林網VEVB"
print(str1.isupper())
str1 = "武林網it樂園"
print(str1.isupper())
str1 = "He is a Good Boy./r/n他是一個好男孩。"
print(str1.isupper())
str1 = "HELLO,2020"
print(str1.isupper())
str1 = "(@T@)"
print(str1.isupper())
以上在Python3.8.2中的執行情況如下圖所示:
以上示例說明,不管字符串中含有什么字符,但只要字符串中所有字母形式的字符是大寫形式,isupper() 函數就會輸出 True ,否則就是 False .
4、不包含字母的情況
str1 = "" #空字符串
print(str1.isupper())
str1 = " " # 僅有三個空格
print(str1.isupper())
str1 = "202006210611" # 僅包含數字
print(str1.isupper())
str1 = "@$%#&" # 僅包含特殊字符
print(str1.isupper())
str1 = "十四五規劃" # 僅包含漢字
print(str1.isupper())
str1 = "/t/r/v/r/n" # 非字母的轉義字符
print(str1.isupper())
以上程序的輸出結果形式如下圖所示:
以上示例表明,字符串中如果不含任何字母時,isupper() 函數一律輸出 False.
Python中 isupper() 函數輸出結果的要點在于:
(1)如果一個字符串中不含Unicode字符庫中定義的任何字母形式的字符,則isupper()函數輸出一定為 False,其中包含空字符串,空白字符,格式控制符以及非字母的文字字符;
(2)如果一個字符串中含有Unicode字符庫中定義的字母形式的字符,但字符串中至少包含一個字母的小寫形式時,isupper() 函數一定輸出 False;
(3)只有當字符串中含有Unicode字符庫中定義的字母形式的字符,且所有字母形式的字符為大寫形式時,isupper()函數才會輸出 True.
|
新聞熱點
疑難解答