python字符串和列表(数组)之间的相互转换,本文将介绍python如何将str转换为list以及如何将list转为str
第一种
str1 = "zheshi yige zifuchuan"
list1 = list(str1)
输出
print(list1)
['z', 'h', 'e', 's', 'h', 'i', ' ', 'y', 'i', 'g', 'e', ' ', 'z', 'i', 'f', 'u', 'c', 'h', 'u', 'a', 'n']
第二种
str2 = "zhe shi ling yige zifuchuan"
list2 = str2.split()
list3 = str2.split(" ")
输出
print(list2)
['zhe', 'shi', 'ling', 'yige', 'zifuchuan']
print(list3)
['zhe', 'shi', 'ling', 'yige', 'zifuchuan']
list4 = ['h', 'e', 'l', 'l', 'o']
str4 = "".join(list4)
输出
print(str4)
hello
viencoding.com版权所有,允许转载,但转载请注明出处和原文链接: https://viencoding.com/article/165