Python 字符串 istitle() 使用方法及示例

由网友 大卫 发布 阅读 5

Python 字符串 istitle() 使用方法及示例

Python 字符串方法

检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写,如果是,则istitle()返回True。如果不是,则返回False。

istitle()方法的语法为:

string.istitle()

istitle()参数

istitle()方法不带任何参数。

istitle()返回值

istitle()方法返回:

  • 如果字符串中所有的单词拼写首字母为大写,且其他字母为小写则返回 True,否则返回 False.

示例1:istitle()的工作

s = 'Python Is Good.'
print(s.istitle())

s = 'Python is good'
print(s.istitle())

s = 'This Is @ Symbol.'
print(s.istitle())

s = '99 Is A Number'
print(s.istitle())

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

运行该程序时,输出为:

True
False
True
True
False

示例2:如何使用istitle()?

s = 'I Love Python.'
if s.istitle() == True:
  print('istitle()为true')
else:
  print('istitle()为false')
  
s = 'PYthon'
if s.istitle() == True:
  print('istitle()为true')
else:
  print('istitle()为false')

运行该程序时,输出为:

istitle()为true
istitle()为false

Python 字符串方法

Python 字符串 islower() Python 字符串 isdigit()