python中join方法的作用
join()方法是Python中的一个字符串方法,它将一个字符串列表连接起来,返回一个以指定分隔符分隔的字符串。下面是一个例子:
list1 = ['1', '2', '3'] print('*'.join(list1)) # 输出:1*2*3
在这个例子中,我们使用join()方法将列表中的字符串连接起来,并使用星号作为分隔符。另外,join()方法也可以用于连接其他类型的序列,例如元组和集合。
tuple1 = ('a', 'b', 'c') print('-'.join(tuple1)) # 输出:a-b-c set1 = {'hello', 'world', 'python'} print(' '.join(set1)) # 输出:hello world python
12
引用