python3 字符串转大写
时间: 2023-09-29 10:06:34 浏览: 128
Python3 中可以使用字符串对象的 `upper()` 方法将字符串转换为大写形式。例如:
```
s = "hello world"
s_upper = s.upper()
print(s_upper) # 输出 "HELLO WORLD"
```
在这个例子中,我们将字符串 `s` 转换为大写形式并将结果存储在变量 `s_upper` 中。然后使用 `print()` 函数将结果输出到控制台。
相关问题
python字符串转大写
在Python中,可以使用字符串的`upper()`方法将字符串转换为大写。`upper()`方法返回一个新的字符串,其中所有的字母都被转换为大写形式。
以下是一个示例代码:
```python
string = "hello world"
uppercase_string = string.upper()
print(uppercase_string)
```
输出结果为:
```
HELLO WORLD
```
将Python字符串中大写转换小写小写转换大写
在Python中,将字符串的大写字母转换为小写字母可以使用`lower()`方法,而将小写字母转换为大写字母则可以使用`upper()`方法。以下是这两个操作的简单示例:
```python
# 转换为小写
string_lower = "HELLO WORLD".lower()
print(string_lower) # 输出: hello world
# 转换为大写
string_upper = "hello world".upper()
print(string_upper) # 输出: HELLO WORLD
```
如果你想在原始字符串上就地修改而不创建新字符串,可以这样做:
```python
# 就地修改为小写
string_in_place = "HELLO WORLD"
string_in_place = string_in_place.lower()
print(string_in_place) # 输出: hello world
# 就地修改为大写
string_in_place = "hello world"
string_in_place = string_in_place.upper()
print(string_in_place) # 输出: HELLO WORLD
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)