Python中大小写字母转换

Python022

Python中大小写字母转换,第1张

1. 在python中主要有四种转换方式,如下图

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

利用capitalize()函数将字符串的首字母转为大写,其余变为小写def normallize(name):return name.capitalize()L2 = list(map(normallize,L1))print(L2)