• 技术文章 >常见问题 >Python常见问题

    Python中常见的字符串拼接方法有哪些?

    宋雪维宋雪维2020-12-11 17:33:58原创3937

    针对大家在Python中常常遇到字符串拼接的问题,本文主要向大家介绍Python中常见的六种拼接方法:加号法、逗号法、直接拼接法、格式化法、join函数法、多行字符串拼接法。详情如下:

    1、加号法

    使用简单直接,但这种方法效率低

    1

    website  =  'python'  'tab'  '.com'

    2、逗号法

    字符串之间会多出一个空格

    1

    2

    str_a = 'python'

    print('hello', str_a, '!')

    输出

    1

    hello python !

    3、直接拼接法

    Python独有拼接法,只能用于字符串的拼接,不能用于变量拼接

    1

    2

    3

    4

    5

    #code

    print('abc''xyz')

      

    #output

    adcxyz

    4、格式化法:使用%或者format进行拼接

    1

    2

    3

    4

    >>> text1 = "Hello"

    >>> text2 = "World"

    >>> "%s%s"%(text1,text2)

    'HelloWorld'

    5、join函数法

    1

    2

    listStr  =  [ 'python' 'tab' '.com' ]

    website  =  ''.join(listStr)

    6、多行字符串拼接法

    1

    2

    3

    4

    5

    6

    7

    8

    >>> text = ('666'

     '555'

     '444'

     '333')

    >>> print(text)

    666555444333

    >>> print (type(text))

    <class 'str'>

    以上就是Python字符串拼接的常见六种方法,大家可以根据个人需求选择不同方法拼接,大家可以直接套用哦~

    专题推荐:python中字符串拼接
    上一篇:如何使用python中join()函数实现字符串拼接? 下一篇:Python中字符串与二进制如何相互转换?

    相关文章推荐

    • python线程强制停止工作• python中local本地对象• 如何使用python中join()函数实现字符串拼接?• python中条件锁与信号量锁的使用方法• python self是什么意思?怎么使用?

    全部评论我要评论

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

  • 取消发布评论
  • 

    Python学习网