python str和list的相互转换

1.字符串转化为列表类型

1print(list('winter is coming'))
2 输出结果:['w', 'i', 'n', 't', 'e', 'r', ' ', 'i', 's', ' ', 'c', 'o', 'm', 'i', 'n', 'g']
3 
4 
5 #利用S.split(sep,maxsplit=-1),如果参数为空,任何whitespace类型皆为分隔符
6 print(('winter is coming').split())
7 输出结果:['winter', 'is', 'coming']

2.列表转化为字符串

1 #利用sep.join(iterable)
2 print(' '.join(['winter','is','coming!']))
3 输出结果:winter is coming!

 

posted on 2018-05-13 12:28  baby_step  阅读(285)  评论(0)    收藏  举报

导航