答案:Python 3 提供多种字符串方法处理大小写,1. str.upper() 将字符串转为大写;2. str.lower() 转为小写;3. str.title() 实现首字母大写;4. str.swapcase() 互换大小写;5. isupper()、islower()、istitle() 用于判断大小写状态。

在 Python 3 中,字母大小写可以通过字符串方法和内置函数来处理。下面是一些常用的操作方式:
1. 转换为大写(uppercase)
使用 str.upper() 方法将字符串中的所有小写字母转换为大写。示例:
立即学习“Python免费学习笔记(深入)”;
text = "hello world"print(text.upper()) # 输出: HELLO WORLD
2. 转换为小写(lowercase)
使用 str.lower() 方法将所有大写字母转换为小写。示例:
立即学习“Python免费学习笔记(深入)”;
text = "Hello World"print(text.lower()) # 输出: hello world
3. 首字母大写(title case)
使用 str.title() 方法将每个单词的首字母变为大写。示例:
立即学习“Python免费学习笔记(深入)”;
text = "hello world"print(text.title()) # 输出: Hello World
4. 大小写互换(swap case)
使用 str.swapcase() 方法将大写变小写,小写变大写。示例:
立即学习“Python免费学习笔记(深入)”;
text = "HeLLo WoRLd"print(text.swapcase()) # 输出: hEllO wOrlD
5. 判断大小写
可以使用以下方法检查字符或字符串的大小写状态:- str.isupper():判断是否全部为大写
- str.islower():判断是否全部为小写
- str.istitle():判断是否为首字母大写格式
示例:
立即学习“Python免费学习笔记(深入)”;
text1 = "HELLO"text2 = "hello"
print(text1.isupper()) # 输出: True
print(text2.islower()) # 输出: True
基本上就这些常见用法,根据需要选择对应的方法即可。










