📜  Python String istitle() 方法

📅  最后修改于: 2022-05-13 01:55:31.180000             🧑  作者: Mango

Python String istitle() 方法

Python String istitle() 方法如果字符串是标题字符串,则返回 True,否则返回 False。什么是标题?每个单词的第一个字符为大写,其余所有字符为小写字母的字符串。

示例 1

Python3
# First character in each word is 
# uppercase and remaining lowercases
s = 'Geeks For Geeks'
print(s.istitle())
  
# First character in first
# word is lowercase
s = 'geeks For Geeks'
print(s.istitle())
  
# Third word has uppercase
# characters at middle
s = 'Geeks For GEEKs'
print(s.istitle())
  
s = '6041 Is My Number'
print(s.istitle())
  
# word has uppercase
# characters at middle
s = 'GEEKS'
print(s.istitle())


Python3
s = 'I Love Geeks For Geeks'
  
if s.istitle() == True:
    print('Titlecased String')
else:
    print('Not a Titlecased String')
  
s = 'I Love geeks for geeks'
  
if s.istitle() == True:
    print('Titlecased String')
else:
    print('Not a Titlecased String')


输出:

True
False
False
True
False

示例 2

Python3

s = 'I Love Geeks For Geeks'
  
if s.istitle() == True:
    print('Titlecased String')
else:
    print('Not a Titlecased String')
  
s = 'I Love geeks for geeks'
  
if s.istitle() == True:
    print('Titlecased String')
else:
    print('Not a Titlecased String')

输出:

Titlecased String
Not a Titlecased String