3. 将字符串s = "alex"转换成列表与元组 s = "alex" 将列表与元组进行相互转换
时间: 2023-05-28 18:03:30 浏览: 220
解决python文件字符串转列表时遇到空行的问题
将字符串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']
```
阅读全文