在Python中,字符串的大小写转换可以通过以下几种内置方法实现:
1. `upper()` 方法:将字符串中的所有小写字母转换为大写字母。
text = "hello, world!"
print(text.upper()) 输出:HELLO, WORLD!
2. `lower()` 方法:将字符串中的所有大写字母转换为小写字母。
text = "HELLO, WORLD!"
print(text.lower()) 输出:hello, world!
3. `capitalize()` 方法:将字符串的首字母转换为大写,其余字母转换为小写。
text = "hello world"
print(text.capitalize()) 输出:Hello world
4. `title()` 方法:将字符串中每个单词的首字母转换为大写,其余字母转换为小写。
text = "i love python"
print(text.title()) 输出:I Love Python
5. `swapcase()` 方法:将字符串中的大写字母转换为小写字母,小写字母转换为大写字母。
text = "Hello World"
print(text.swapcase()) 输出:hELLO wORLD