join()函数可以将元组、列表、字典转换成字符串格式,但是本次的2个小栗子执行之后,却报错。
list1 = [1, 'two', 'three', 4]
tup1 = (1, 'two', 'three', 4)
print(' '.join( list1 ))
print(' '.join( tup1 ))
会报错“TypeError: sequence item 0: expected str instance, int found”
百度了一下,因为列表和元组里包含了数字,所以无法直接转换成字符串,必须是这种格式才行 [“1”, 'two', 'three', ”4”]。
或者进行格式化输出。
list1 = [1, 'two', 'three', 4]
print(" ".join('%s' %id for id in list1))