版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
先看下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