Python Python中List和字符串类型的相互转换

kevensuu · 2019-12-20 22:40:40 · 热度: 16

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为字符串

此外,引号中是字符之间的分割符,如 , 等等

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册