Python 中的 istitle() 函数用于检查字符串是否为标题格式。如果是,则返回 True,否则返回 False。标题格式是指每个单词的首字母为大写,其余字母为小写。
string.istitle()
istitle() 方法不接受任何参数。在检查字符串时,所有符号和数字都会被忽略。
返回值为布尔值。如果字符串为空、为纯数字字符串或只包含符号的字符串,则函数返回 False。
| 输入 | 返回值 |
|---|---|
| 标题格式字符串 | True |
| 非标题格式字符串 | False |
string = 'Hi How Are You'
print(string.istitle())
string = 'Hi how are you'
print(string.istitle())
string = 'Hi How Are You?'
print(string.istitle())
string = '121 Hi How Are You'
print(string.istitle())
string = 'HOW ARE YOU'
print(string.istitle())
输出
True False True True False
string = 'Python programming'
if string.istitle() == True:
print('It is a Titlecased String')
else:
print('It is not a Titlecased String')
string = 'PYthon PRogramming'
if string.istitle() == True:
print('It is a Titlecased String')
else:
print('It is not a Titlecased String')
输出
It is a Titlecased String It is not a Titlecased String