Python字符串isupper()

时间:2020-02-23 14:43:28  来源:igfitidea点击:

如果所有大小写的字符均为大写,则Python String isupper()函数将返回True。
如果字符串为空或者没有大小写字符,则返回" False"。

Python字符串isupper()

大小写字符是指具有常规类别属性的字符,例如" Lu"(字母大写)," Ll"(字母小写)或者" Lt"(字母大写)。

让我们看一下isupper()函数的一些示例。

s = 'HELLO WORLD'
print(s.isupper())

输出:True,因为所有大小写的字符均为大写。

s = 'Hello World'
print(s.isupper())

输出:False,因为其中有一些小写字符。

s = 'HELLO WORLD 2019'
print(s.isupper())

输出:True,因为所有大小写的字符都为大写。
字符串中的数字不是大小写字符。

s = ''
print(s.isupper())

输出:False,因为字符串为空。

s = '2019'
print(s.isupper())

输出:False,因为字符串没有任何大小写的字符。

让我们看一个带有特殊字符串的示例。

s = 'ÂƁȻ2019'
print(s.isupper())

输出:True,因为字符串中所有大小写的字符均为大写。

打印所有大写字母

这是一个用于打印有关所有大写字母大写字符的信息的简单程序。

import unicodedata

count = 0
for codepoint in range(2 ** 16):
  ch = chr(codepoint)
  if ch.isupper():
      print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
      count = count + 1
print(f'Total Number of Uppercase Unicode Characters = {count}')

输出:

0041: A (LATIN CAPITAL LETTER A)
0042: B (LATIN CAPITAL LETTER B)
0043: C (LATIN CAPITAL LETTER C)
...
ff38: X (FULLWIDTH LATIN CAPITAL LETTER X)
ff39: Y (FULLWIDTH LATIN CAPITAL LETTER Y)
ff3a: Z (FULLWIDTH LATIN CAPITAL LETTER Z)
Total Number of Uppercase Unicode Characters = 1154