📜  Python字符串capitalize()

📅  最后修改于: 2020-09-20 14:28:18             🧑  作者: Mango

在Python,capitalize()方法将字符串的第一个字符转换为大写字母,并将所有其他字符(如果有的话)转换为小写。

capitalize()的语法为:

string.capitalize()

capitalize()参数

capitalize() 函数不带任何参数。

从capitalize()返回的值

capitalize() 函数返回一个字符串 ,该字符串的首字母大写,所有其他字符均小写。它不会修改原始字符串。

示例1:大写句子

string = "python is AWesome."

capitalized_string = string.capitalize()

print('Old String: ', string)
print('Capitalized String:', capitalized_string)

输出

Old String: python is AWesome
Capitalized String: Python is awesome

示例2:非字母的第一个字符

string = "+ is an operator."

new_string = string.capitalize()

print('Old String:', string)
print('New String:', new_string)

输出

Old String: + is an operator.
New String: + is an operator.