本文實例講述了Python實現字符串與數組相互轉換功能。具體如下:
字符串轉數組
字符串轉數組使用split函數
str = '1,2,3'
arr = str.split(',')
print a
數組轉字符串
數組轉字符串使用join函數
# 方法1
arr = ['a','b']
str1 = ','.join(arr)
print str1
# 方法2
arr = [1,2,3]
# str = ','.join(str(i) for i in arr) # 此處str命名與str函數沖突!
str2 = ','.join(str(i) for i in arr)
print str2