python 陣列轉換為string
阿新 • • 發佈:2018-10-31
先看下join函式:
語法為:
str.join(sequence)
sequence -- 要連線的元素序列
例項一:連線字串
str1 = '-'
seq = ("a", "b", "c")
print str1.join(seq)
例項二:連線陣列(陣列元素為字串)
str2 = '*'
seq2 = ["a", "b", "c"]
print str2.join(seq2)
例項三:連線陣列(陣列元素為數字)
錯誤示範:
str3 = '*'
seq3 = [1, 2, 3]
print str3.join(seq3)
會觸發TypeError: sequence item 0: expected string, int found的錯誤
正確的示範:
str3 = '*'
seq4 = [1, 2, 3]
seq5 = []
for i in range(len(seq4)):
seq5.append(str(seq4[i]))
print str3.join(seq5)
print str3.join(str(i) for i in seq5)
string的方法列表:
http://www.runoob.com/python/python-strings.html