Tutorial Study Image

Python title()

Python 中的 title() 函数用于返回一个新字符串,其中每个单词的第一个字符应为大写字母,其余为小写字母,就像标题一样。


str.title() 
 

title() 参数

title() 方法不接受任何参数。只有当单词的第一个字符是字母时才会执行转换。如果单词以数字或符号开头,则其后的第一个字母将大写。

title() 返回值

返回值将是原始字符串的副本。title() 也会将撇号后的第一个字符大写。

输入 返回值
字符串 首字母大写的字符串

Python 中 title() 方法的示例

示例 1:title() 在 Python 中如何工作?


string1 = 'my name is jhon 25.'
print(string1.title())

string2 = '45 nm44 *67 find'
print(string2.title())
 

输出


My Name Is Jhon 25
45 Nm44 *67 Find

示例 2:带撇号的 title() 如何工作?


string = "she's a docter, isn't she?"
print(string.title())
 

输出


She'S A Docter, Isn'T She?