Python字符串istitle()

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

如果字符串为大写且不为空,则Python String istitle()返回True,否则返回False。
当字符串的所有单词均以大写字母开头且所有其他字符均为小写字母时,则以标题为大写。
如果字符串不包含任何用大写字母括起来的字符,则此函数还将返回False。

Python字符串istitle()

我们来看一些Python String istitle()函数的示例。

s = 'A Big Cat'
print(s.istitle())

输出:True

s = 'A BIG Cat'
print(s.istitle())

输出:由于" BIG"而产生的" False"。

s = 'A big Cat'
print(s.istitle())

输出:由于"大",所以为" False"。

s = 'a big cat'
print(s.istitle())

输出:False,因为单词不是以大写字母开头。

s = 'A'
print(s.istitle())

输出:True

s = ''
print(s.istitle())

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

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

输出:False,因为字符串不包含任何带大写字母的字符。

让我们看一个示例,其中字符串包含特殊字符。

s = 'Â Ɓig Ȼat'
print(s.istitle())

输出:True,因为Â,Ɓ和Ȼ是标题区分大小写的字母。

打印所有Unicode标题大写的字符

这是一个简单的代码段,用于打印所有带有标题大小写的Unicode字符。

import unicodedata

count = 0
for codepoint in range(2 ** 16):
  ch = chr(codepoint)
  if ch.istitle():
      print(u'{:04x}: {} ({})'.format(codepoint, ch, unicodedata.name(ch, 'UNNAMED')))
      count = count + 1
print(f'Total Number of Title 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 Title Unicode Characters = 1185