1. 程式人生 > >python字符串與列表的相互轉換

python字符串與列表的相互轉換

列表 log style div play gpo none split spl

1.字符串轉列表

2.列表轉字符串

1. 字符串轉列表

s =‘hello python !‘
li = s.split(‘ ‘) #註意:引號內有空格
print (li)
輸出:
[‘hello‘, ‘python‘, ‘!‘]

2. 列表轉字符串

li = [‘hello‘, ‘python‘, ‘!‘]
s = ‘ ‘.join(li) #註意:引號內有空格
print(s)

輸出:
hello python !

 

python字符串與列表的相互轉換