Python字符串大写转小写

Python字符串大写转小写教程

在开发过程中,很多时候我们需要将一个 字符串 的大写形式全部转换成小写的需求,在 Python 中,将某个字符串的大写字符转成小写使用的函数为 lower() 。

Python lower()函数详解

语法

S.lower() -> str

参数

参数 描述
s 表示原字符串。

返回值

全部转成小写之后的字符串。

案例

将字符串转成小写

使用 lower() 函数,将字符串转成小写

print("嗨客网(www.haicoder.net)") # 使用 lower() 函数,将字符串转成小写 strHaicoder = "Study Python From HaiCoder" print("strHaicoder =", strHaicoder.lower())

程序运行后,控制台输出如下:

38 python字符串转小写.png

首先,我们定义了一个字符串类型的 变量 strHaicoder,接着我们使用字符串的 lower() 函数将变量 strHaicoder 的字符全部转成小写,并使用 print() 函数打印最终转换后的结果。

字符串本来就全部小写

使用 lower() 函数,将字符串转成小写

print("嗨客网(www.haicoder.net)") # 使用 lower() 函数,将字符串转成小写 strHaicoder = "study python from haicoder" print("strHaicoder =", strHaicoder.lower())

程序运行后,控制台输出如下:

39 python字符串转小写.png

首先,我们定义了一个字符串类型的变量 strHaicoder,接着我们使用字符串的 lower() 函数将变量 strHaicoder 的字符全部转成小写,并使用 print() 函数打印最终转换后的结果。

因为字符串 strHaicoder 的所有字符原来就是全部小写,所以这里没有做任何的转换。

转换包含中文的字符串

使用 lower() 函数,转换包含中文的字符串

print("嗨客网(www.haicoder.net)") # 使用 lower() 函数,转换包含中文的字符串 strHaicoder = "嗨客网(HaiCoder)" print("strHaicoder =", strHaicoder.lower())

程序运行后,控制台输出如下:

40 python字符串转小写.png

首先,我们定义了一个字符串类型的变量 strHaicoder,并赋值为 “嗨客网(HaiCoder)”, 接着我们使用字符串的 lower() 函数将变量 strHaicoder 转成小写,并使用 print() 函数打印最终转换后的结果。

因为字符串 strHaicoder 包含中文,所以中文并没有做任何的改变,而其中的所有的英文大写字母全部被转成了小写,所以最终输出了 “嗨客网(haicoder)”。

Python字符串大写转小写总结

在 Python 中,将某个字符串的大写字符转成小写使用的函数为 lower() 。Python lower() 函数语法:

S.lower() -> str