Python字符串 isupper() 方法
Python String isupper() 方法返回字符串中的所有字符是否都是大写的。
Syntax:
string.isupper()
Parameters:
The isupper() method doesn’t take any parameters.
Returns:
True if all the letters in the string are in the upper case and False if even one of them is in the lower case.
示例 1:演示 isupper() 的工作原理
Python3
# 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()))
Python3
# 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))
输出:
Is GEEKSFORGEEKS full uppercase ? : True
Is Geeksforgeeks full uppercase ? : False
示例 2:实际应用
这个函数可以有多种使用方式,有很多实际应用。一种这样的应用程序用于检查大写,检查缩写(通常是大写),检查需要所有大写的句子的正确性。下面演示的是一个小例子,展示了 isupper() 方法的应用。
Python3
# 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