1.str >>>list

str1 = "12345"  
list1 = list(str1)  
print list1  

str2 = "123 sjhid dhi"  
list2 = str2.split() #or list2 = str2.split(" ")  
print list2  

str3 = "www.google.com"  
list3 = str3.split(".")  
print list3  
輸出為:  
['1', '2', '3', '4', '5']  
['123', 'sjhid', 'dhi']  
['www', 'google', 'com']  

2.list >>>str

str4 = "".join(list3)  
print str4  
str5 = ".".join(list3)  
print str5  
str6 = " ".join(list3)  
print str6  
輸出為:
wwwgooglecom  
www.google.com  
www google com  

python中有三個內建函式:列表,元組和字串,他們之間的互相轉換使用三個函式,str(),tuple()和list(),具體示例如下所示:

s = “xxxxx”
list(s)
[‘x’, ‘x’, ‘x’, ‘x’, ‘x’]
tuple(s)
(‘x’, ‘x’, ‘x’, ‘x’, ‘x’)
tuple(list(s))
(‘x’, ‘x’, ‘x’, ‘x’, ‘x’)
list(tuple(s))
[‘x’, ‘x’, ‘x’, ‘x’, ‘x’]
列表和元組轉換為字串則必須依靠join函式
“”.join(tuple(s))
‘xxxxx’
“”.join(list(s))
‘xxxxx’
str(tuple(s))
“(‘x’, ‘x’, ‘x’, ‘x’, ‘x’)”

參考連結:

.