Python 字符串转小写:简单易懂的代码示例
Python 字符串转小写:简单易懂的代码示例
在 Python 中,您可以轻松地将字符串转换为小写。使用 lower() 方法可以将所有大写字母转换为小写字母。
代码示例:
string = 'Hello, World!'
lowercase_string = string.lower()
print(lowercase_string)
输出结果:
hello, world!
解释:
string = 'Hello, World!':定义一个名为string的变量,并将其赋值为字符串 'Hello, World!'。lowercase_string = string.lower():调用lower()方法将string变量中的所有大写字母转换为小写字母,并将结果存储在lowercase_string变量中。print(lowercase_string):将lowercase_string变量的值(即转换后的字符串)打印到控制台。
总结:
lower() 方法是 Python 中处理字符串大小写的强大工具。通过使用它,您可以轻松地将字符串转换为小写,从而在需要时确保一致性。
原文地址: https://www.cveoy.top/t/topic/lZr6 著作权归作者所有。请勿转载和采集!