在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"
以上方法都会返回一个新的字符串,原始字符串不会被修改
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://sigusoft.com/bj/52466.html