Python String isupper() 方法用于确定给定字符串的所有基于大小写的字符或字母是否为大写。大小写字符是具有常规类别属性的字符,该属性是 “Ll” (字母,小写) 、 “Lu” (字母,大写) 或 “Lt” (字母,首字母大写) 之一。
大写定义为以大写形式书写或打印的字母,也称为大写字母。
例如,如果给定单词 “ProGRaMmINg” 中的大写字母是 P、G、R、M、I 和 N。
注意:不检查给定字符串中的数字、符号和空格,只考虑字母字符。
语法
以下是 Python String isupper() 方法的语法:
str.isupper()
参数
此方法不接受任何参数。
返回值
如果字符串中的所有大小写字符都是大写的,并且至少应该有一个大小写字符,则此方法返回 true,否则返回 false。
例当我们以大写形式传递字符串中的所有字母时,它将返回 True。
在以下示例中,将创建一个字符串 'PYTHON',其中所有字母均为大写。然后使用 Python Sring isupper() 方法检查给定的字符串是否为大写。然后检索结果。
# initializing the string
Given_string = "PYTHON"
# Checking whether the string is in uppercase
res = Given_string.isupper()
# Printing the result
print ("Is the given string in uppercase?: ", res)
上述代码的输出如下:
Is the given string in uppercase?: True
例
如果字符串中的所有字母都是小写的,则返回 False。
下面是一个字符串 'this is string example....wow!!' 创建。然后使用 isupper() 方法检索结果,说明给定字符串是否为大写。
# initialize the string
str = "this is string example....wow!!!";
print (str.isupper())
以下是上述代码的输出:
False
例
在下面给出的示例中,给定句子中所有大写单词的计数是使用 for 循环计算的。首先,创建一个字符串。然后拆分字符串,并通过计算大写单词来检索结果:
# initializing the string
Given_string = "python IS a PROGRAMMING language"
res = Given_string.split()
count = 0
# Counting the number of uppercase
for i in res:
if (i.isupper()):
count = count + 1
# Printing the result
print ("The number of uppercase words in the sentence is: ", str(count))
在执行上述代码时,我们得到以下输出:
The number of uppercase words in the sentence is: 2
例
在以下示例中,将创建一个包含数字、符号和字母的字符串,以检查它是否为大写:
# initializing the string
Given_string = "Cod3iN#g"
# Checking whether the string is in uppercase
res = Given_string.isupper()
# Printing the result
print ("Is the given string in uppercase?: ", res)
当我们运行上述程序时,它会产生以下结果:
Is the given string in uppercase?: False