python 数组转换为string

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liangxy2014/article/details/79155433

先看下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

猜你喜欢

转载自blog.csdn.net/liangxy2014/article/details/79155433
今日推荐