capitalize()将字符串的第一个字母变成大写,其他字母变小写。
str = "this is string example from runoob....wow!!!"
print("str.capitalize() : ", str.capitalize())
结果:
str.capitalize() : This is string example from runoob....wow!!!
swapcase() 方法用于对字符串的大小写字母进行转换。
str = "this is string example....wow!!!"
print (str.swapcase())
str = "This Is String Example....WOW!!!"
print (str.swapcase())
结果:
THIS IS STRING EXAMPLE....WOW!!!
tHIS iS sTRING eXAMPLE....wow!!!
title() 方法返回"标题化"的字符串,就是说所有单词的首个字母转化为大写,其余字母均为小写
str = "this is strinG example from runooB....wow!!!"
print (str.title())
结果:
This Is String Example From Runoob....Wow!!!
# 非字母后的第一个字母将转换为大写字母:
txt = "hello b2b2b2 and 3g3g3g"
x = txt.title()
print(x)
结果:
Hello B2B2B2 And 3G3G3G
如果字符串两端和中间有多个空格,建议使用string模块中的capwords()函数。
from string import capwords
s = ' hello word!'
print(s.title())
print(capwords(s))
结果:
Hello Word!
Hello Word!