1. 程式人生 > >Python中List和字符串類型的相互轉換

Python中List和字符串類型的相互轉換

相互轉換 join world 字符串類型 類型 引號 style spa 字符串類

1.字符串轉換成List

a = Hello World!
a_list = list(a) 
//[‘H‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘ ‘, ‘W‘, ‘o‘, ‘r‘, ‘l‘, ‘d‘, ‘!‘]

其中a為字符串,a_list為List

2.List轉換成字符串

a_list = [H, e, l, l, o,  , W, o, r, l, d‘, ‘!‘]
a = ‘‘.join(a_list) //‘Hello Horld!‘

其中a_list為List,a為字符串

此外,引號中是字符之間的分割符,如‘,’,‘\t‘等等

Python中List和字符串類型的相互轉換