str.join(iterable) 传入的参数必须需要是可迭代对象
#当元组中的元素类型是字符串类型是,可直接将数组作为参数传入到join()中
a=('hello','python3')
b=''.join(a)
print(b)
运行结果为:
hellopython3
#当元组中的元素类型是整型时,会报错
a=(1,2,3)
#b=''.join(a) #TypeError: sequence item 0: expected str instance, int found
#b=''.join(a[0]) #TypeError: can only join an iterable
b=''.join(str(a[0]))
print(b)
运行结果为:
1
注:注意一下两种方式的区别
l=['a','b','c']
s=str(l)
ss=''.join(l)
print(s)
print(type(s))
print(ss)
print(type(ss))
运行结果为:
['a', 'b', 'c']
<class 'str'>
abc
<class 'str'>