• 技术文章 >Python技术 >Python基础教程

    实例讲解join方法的使用

    silencementsilencement2019-07-09 16:05:25原创2099

    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
    专题推荐:join
    品易云
    上一篇:鲜为人知的python位运算 下一篇:Python如何实现定时发送qq消息

    相关文章推荐

    • 加速Python程序运行• 超详细的介绍Python语句• python中的装饰器的使用实战

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网