在Python中,可以使用内置的字符串方法来转换字母的大小写。以下是几种常用的方法:
1. `upper()` 方法:将字符串中的所有字母转换为大写字母。
string = "hello world"
new_string = string.upper()
print(new_string) 输出 "HELLO WORLD"
2. `lower()` 方法:将字符串中的所有字母转换为小写字母。
string = "HELLO WORLD"
new_string = string.lower()
print(new_string) 输出 "hello world"
3. `capitalize()` 方法:将字符串的首字母转换为大写字母,其余字母转换为小写字母。
string = "hello world"
new_string = string.capitalize()
print(new_string) 输出 "Hello world"
4. `title()` 方法:将字符串中每个单词的首字母转换为大写字母,其余字母转换为小写字母。
string = "hello world"
new_string = string.title()
print(new_string) 输出 "Hello World"
5. `swapcase()` 方法:将字符串中的大写字母转换为小写字母,小写字母转换为大写字母。
string = "Hello World"
new_string = string.swapcase()
print(new_string) 输出 "hELLO wORLD"
以上方法都会返回一个新的字符串,原始字符串不会被修改