温馨提示×

python怎么修改字符串中的字符大小

小亿
84
2024-05-28 13:53:08
栏目: 编程语言

要修改字符串中字符的大小,可以使用字符串的 upper()lower()capitalize() 方法。

  1. 使用 upper() 方法将字符串中的所有字符都转换为大写:
string = "hello"
new_string = string.upper()
print(new_string)  # 输出: HELLO
  1. 使用 lower() 方法将字符串中的所有字符都转换为小写:
string = "WORLD"
new_string = string.lower()
print(new_string)  # 输出: world
  1. 使用 capitalize() 方法将字符串的第一个字符转换为大写,其他字符转换为小写:
string = "hello world"
new_string = string.capitalize()
print(new_string)  # 输出: Hello world

0