當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python string isupper()用法及代碼示例

Python在其庫中具有許多實用程序函數,這些函數始終可以幫助我們完成一些日常工作。讓我們來看看字符串的工作原理isupper()該方法實際上檢查字符串中的所有字符是否均為大寫。

用法: string.isupper()

參數:沒有


返回:如果字符串中的所有字母均為大寫,則為true;如果甚至其中之一為小寫,則為False。

代碼1:演示isupper()的工作

# Python3 code to demonstrate 
# working of isupper() 
  
# initializing string  
isupp_str = "GEEKSFORGEEKS"
not_isupp = "Geeksforgeeks"
  
# Checking which string is  
# completely uppercase 
print ("Is GEEKSFORGEEKS full uppercase ?:" + str(isupp_str.isupper())) 
print ("Is Geeksforgeeks full uppercase ?:" + str(not_isupp.isupper()))

輸出:

Is GEEKSFORGEEKS full uppercase ?:True
Is Geeksforgeeks full uppercase ?:False

實際應用:此函數可以以多種方式使用,並且具有許多實際應用。一種這樣的應用程序,用於檢查大寫字母,檢查縮寫(通常為大寫字母),檢查需要所有大寫字母的句子的正確性。下麵展示的是一個小示例,顯示了isupper()方法的應用。

代碼2:演示isupper()的實際應用

# Python3 code to demonstrate 
# application of isupper() 
  
# checking for abbreviations. 
# short form of work/phrase 
test_str = "Cyware is US based MNC and works in IOT technology"
  
# splitting string  
list_str = test_str.split() 
  
count = 0
  
# counting upper cases 
for i in list_str:
    if (i.isupper()):
        count = count + 1
  
# printing abbreviations count  
print ("Number of abbreviations in this sentence is:" + str(count))

輸出:

Number of abbreviations in this sentence is:3


相關用法


注:本文由純淨天空篩選整理自manjeet_04大神的英文原創作品 Python String | isupper() method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。