Python的join()方法用于将序列中的元素以指定的字符连接生成一个新的字符串
语法
str.join(sequence)
参数
sequence 要连接的元素序列、字符串、元组、字典
返回值
返回通过指定字符连接序列中的元素后生成的新的字符串
实例
str = "-"; seq = ("a", "b", "c") #字符串序列 print str.join(seq)
输出结果为
a-b-c
实例2
#1)对序列进行操作 LIST =["I","am","bigship","come","from","China"] print (' '.join(LIST)) #2)对字符串进行操作 str="i am bigship" print('->'.join(str)) #3)对元组进行操作 TUPLE = ("I","am","bigship","come","from","China") print(' '.join(TUPLE)) #4)对字典进行操作 Dir ={'b':1,'i':2,'g':3,'s':4,'h':5,'p':7} print( ':'.join(Dir))
输出结果为:
I am bigship come from China i-> ->a->m-> ->b->i->g->s->h->i->p I am bigship come from China b:i:g:s:h:plist = ['a','b','c','d'] print('+'.join(list)) 输出结果为: a+b+c+d