溫馨提示×

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

小億
104
2024-05-28 13:53:08
欄目: 編程語言

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

  1. 使用 upper() 方法將字符串中的所有字符都轉(zhuǎn)換為大寫:
string = "hello"
new_string = string.upper()
print(new_string)  # 輸出: HELLO
  1. 使用 lower() 方法將字符串中的所有字符都轉(zhuǎn)換為小寫:
string = "WORLD"
new_string = string.lower()
print(new_string)  # 輸出: world
  1. 使用 capitalize() 方法將字符串的第一個(gè)字符轉(zhuǎn)換為大寫,其他字符轉(zhuǎn)換為小寫:
string = "hello world"
new_string = string.capitalize()
print(new_string)  # 輸出: Hello world

0