python将字符串s = "alex"转换成列表与元组 s = "alex" 将列表与元组进行相互转换
时间: 2024-06-11 21:06:59 浏览: 8
将字符串转换成列表:
```
s = "alex"
lst = list(s)
print(lst)
```
输出结果为:
```
['a', 'l', 'e', 'x']
```
将字符串转换成元组:
```
s = "alex"
tup = tuple(s)
print(tup)
```
输出结果为:
```
('a', 'l', 'e', 'x')
```
将列表转换成元组:
```
lst = ['a', 'l', 'e', 'x']
tup = tuple(lst)
print(tup)
```
输出结果为:
```
('a', 'l', 'e', 'x')
```
将元组转换成列表:
```
tup = ('a', 'l', 'e', 'x')
lst = list(tup)
print(lst)
```
输出结果为:
```
['a', 'l', 'e', 'x']
```
相关问题
3. 将字符串s = "alex"转换成列表与元组 s = "alex" 将列表与元组进行相互转换
将字符串s = "alex"转换成列表:
```python
s = "alex"
lst = list(s)
print(lst) # ['a', 'l', 'e', 'x']
```
将字符串s = "alex"转换成元组:
```python
s = "alex"
tup = tuple(s)
print(tup) # ('a', 'l', 'e', 'x')
```
将列表转换成元组:
```python
lst = ['a', 'l', 'e', 'x']
tup = tuple(lst)
print(tup) # ('a', 'l', 'e', 'x')
```
将元组转换成列表:
```python
tup = ('a', 'l', 'e', 'x')
lst = list(tup)
print(lst) # ['a', 'l', 'e', 'x']
```
python将字符串,列表,集合转化为元组
可以使用 Python 的内置函数 `tuple()` 将字符串、列表和集合转换为元组。
例如,将一个字符串转换为元组:
```python
my_string = "Hello, World!"
my_tuple = tuple(my_string)
print(my_tuple)
```
输出:
```
('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!')
```
将一个列表转换为元组:
```python
my_list = [1, 2, 3, 4, 5]
my_tuple = tuple(my_list)
print(my_tuple)
```
输出:
```
(1, 2, 3, 4, 5)
```
将一个集合转换为元组:
```python
my_set = {6, 7, 8, 9, 10}
my_tuple = tuple(my_set)
print(my_tuple)
```
输出:
```
(6, 7, 8, 9, 10)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)