Python 字符串转小写:简单易懂的代码示例

在 Python 中,您可以轻松地将字符串转换为小写。使用 lower() 方法可以将所有大写字母转换为小写字母。

代码示例:

string = 'Hello, World!'
lowercase_string = string.lower()
print(lowercase_string)

输出结果:

hello, world!

解释:

  1. string = 'Hello, World!':定义一个名为 string 的变量,并将其赋值为字符串 'Hello, World!'。
  2. lowercase_string = string.lower():调用 lower() 方法将 string 变量中的所有大写字母转换为小写字母,并将结果存储在 lowercase_string 变量中。
  3. print(lowercase_string):将 lowercase_string 变量的值(即转换后的字符串)打印到控制台。

总结:

lower() 方法是 Python 中处理字符串大小写的强大工具。通过使用它,您可以轻松地将字符串转换为小写,从而在需要时确保一致性。

Python 字符串转小写:简单易懂的代码示例

原文地址: https://www.cveoy.top/t/topic/lZr6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录