python 字符串 istitle() 方法用于检查字符串是否为 titlecased 字符串。 如果 c 字符串是 titlecased 字符串并且至少有一个字符,则此方法返回 true。 大写字符只能跟在无大小写的字符后面,小写字符只能跟在大写字符后面。如果遵循此规则,则此方法返回 true。否则,它将返回 false。
让我们在下一节中了解此方法的更多详细信息。
语法
以下是 python 字符串 istitle() 方法的语法 -
str.istitle()
参数
python 字符串 istitle() 方法不包含任何参数。
返回值如果字符串中的所有单词都是 titlecase 并且 至少是一个字符,否则为 false。
例以下是 python 字符串 istitle() 方法的示例。在这个程序中,创建一个字符串 “qikepu!”,并调用 istitle() 函数来检查输入字符串是否是 titlecased。
str = "qikepu!"
result=str.istitle()
print("Is the input string titlecased?", result)
在执行上述程序时,将生成以下输出 -
Is the input string titlecased? True
例
titlecase 表示小写字母后跟大小写字符,大写字母必须始终后跟不区分大小写的字符。
str = "qikepu!"
result=str.istitle()
print("Is the input string titlecased?", result)
以下是执行上述程序得到的输出 -
Is the input string titlecased? False
例
输入字符串的每个单词都必须具有 titlecase,istitle() 方法才能返回 true。
str = "Hello world"
result=str.istitle()
print("Is the input string titlecased?", result)
通过执行上述程序获得以下输出 -
Is the input string titlecased? False
例
python 字符串 istitle() 方法返回 false,即使创建的字符串的一个单词不是 titleccased。
str = "Hello!Welcome To qikepu."
result=str.istitle()
print("Is the input string titlecased?", result)
上述程序在执行时显示以下输出 -
Is the input string titlecased? False
例
创建的字符串也可以包含数字。但是,在数字后面,只有大写字符必须存在才能将其命名为 titlecase。
str = "123Hello World."
result=str.istitle()
print("Is the input string titlecased?", result)
上述程序的输出显示如下 -
Is the input string titlecased? True