品易云推流 关闭
文章详情页
文章 > Python基础教程 > 实例讲解join方法的使用

实例讲解join方法的使用

头像

silencement

2019-07-09 16:05:252766浏览 · 0收藏 · 0评论

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:p
list = ['a','b','c','d']
print('+'.join(list))

输出结果为:
a+b+c+d
关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图