Python程序教程

您现在的位置是:首页 >  Python

当前栏目

python中 列表(List)转换为字符串(Str)的方法

python,列表,List,转换,字符串,Str,方法
2025-03-28 09:01:09 时间

大家好,又见面了,我是你们的朋友全栈君。

1、List列表转为Str字符串

List中存的是字符串的时候,一般是通过.join()函数去转换:

例 :
    dataList = ['1', '2', '3', '4' ]
    str1 = “ , ” + join(dataList )
    print (dataList)

	结果:
	a b c d 

2、Str转为List列表 主要就是通过str的split()函数,如果为空就用空格标识:

例:
    str1 = 'abcde'
    str2 = 'a b c d   e'
    str3 = 'a, b, c, d, e'
    result1 = list(str1)
    result2 = str2.split()
    result3 = str3.split(', ')
    print(result1)
    print(result2)
    
    结果:
    ['a', 'b', 'c', 'd', 'e']
    ['a', 'b', 'c', 'd', 'e']
    ['a', 'b', 'c', 'd', 'e']

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/137372.html原文链接:https://javaforall.cn