字符串swapcase()方法将给定字符串的所有大写字符转换为小写,并将所有小写字符转换为大写字符,然后将转换后的字符串返回。
swapcase()方法的格式为:
string.swapcase()
注意:不一定是 string.swapcase().swapcase() == string
swapcase()方法不带任何参数。
swapcase()方法返回字符串,其中所有大写字符均转换为小写,小写字符均转换为大写。
# 字符串示例 string = "THIS SHOULD ALL BE LOWERCASE." print(string.swapcase()) string = "this should all be uppercase." print(string.swapcase()) string = "ThIs ShOuLd Be MiXeD cAsEd." print(string.swapcase())
运行该程序时,输出为:
this should all be lowercase. THIS SHOULD ALL BE UPPERCASE. tHiS sHoUlD bE mIxEd CaSeD.