python字符串与列表的相互转换

时间:2023-03-09 04:51:11
python字符串与列表的相互转换

学习内容:

1.字符串转列表

2.列表转字符串

1. 字符串转列表

s ='hello python !'
li = s.split(' ') #注意:引号内有空格
print (li)
输出:
['hello', 'python', '!']

2. 列表转字符串

li = ['hello', 'python', '!']
s = ' '.join(li) #注意:引号内有空格
print(s) 输出:
hello python !