ITPub博客

首页 > IT职业 > IT图书 > 实例讲解join方法的使用

实例讲解join方法的使用

IT图书 作者:shinez 时间:2021-09-11 10:36:10 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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3127/viewspace-2837142/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 如何测试django
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    0
  • 访问量
    933