python字符串大小写
时间: 2023-10-20 21:09:15 浏览: 33
可以使用字符串的方法来实现大小写转换,具体如下:
1. 将字符串全部转换为大写字母:使用 upper() 方法
```python
s = "hello world"
s_upper = s.upper()
print(s_upper) # 输出 "HELLO WORLD"
```
2. 将字符串全部转换为小写字母:使用 lower() 方法
```python
s = "HELLO WORLD"
s_lower = s.lower()
print(s_lower) # 输出 "hello world"
```
3. 将字符串首字母大写:使用 capitalize() 方法
```python
s = "hello world"
s_capitalize = s.capitalize()
print(s_capitalize) # 输出 "Hello world"
```
4. 将字符串每个单词的首字母大写:使用 title() 方法
```python
s = "hello world"
s_title = s.title()
print(s_title) # 输出 "Hello World"
```
相关问题
python 字符串大小写
将字符串转换为大写可以使用字符串对象的 `upper()` 方法,例如:
```python
string = "Hello World"
uppercase_string = string.upper()
print(uppercase_string)
```
输出结果为:
```
HELLO WORLD
```
同样,将字符串转换为小写可以使用字符串对象的 `lower()` 方法,例如:
```python
string = "Hello World"
lowercase_string = string.lower()
print(lowercase_string)
```
输出结果为:
```
hello world
```
Python字符串大小写统计
Python字符串大小写统计可以使用count()函数来实现,例如:
```
text = "Hello, World!"
upper_count = text.count('H') + text.count('W')
lower_count = text.count('e') + text.count('l') + text.count('o') + text.count('r') + text.count('d')
print("大写字母个数为:", upper_count)
print("小写字母个数为:", lower_count)
```
输出结果为:
```
大写字母个数为: 2
小写字母个数为: 8
```
希望这个回答对您有帮助!