2. 这里我们举例说明各自的用途:
#大小写转换
str3 = "Hello"
str4 = "WORLD"
str5 = "shirley.xie"
str6 = "welcome to here"
print(str3.upper()) #转化所有为大写
print(str4.lower()) #转换所有为小写
print("shirley.xie".title()) #转换每个单词首字母为大写
print("welcome to here".capitalize()) #转换第一个首字母大写
输出的结果为:
HELLO
world
Shirley.Xie
Welcome to here
Python大小写转换:1.lower()。将字符串中的大写字母转换成小写字母。语法:str.lower()
2.upper()。将字符串的小写字母转换为大写字母。语法:str.upper()
3.capitalize()。将字符串的第一个字母变成大写,其余字母变为小写。语法:str.capitalize()
4.title()。返回一个满足标题格式的字符串,即所有英文单词首字母大写,其余英文字母小写。语法:str.title()
5.swapcase()。将字符串str中的大小写字母同时进行互换,即将字符串str中的大写字母转换为小写字母,将小写字母转换为大写字母。语法:str.swapcase()。