python字符串的连接和拼接

转载拼接整理内容

1. python字符串的连接   ‘’.join()   

str.join(sequence) sequence -- 要连接的元素序列

序列类型包括字符串、列表、元组、集合和字典

a。列表用 [ ] 标识,是 python 最通用的复合数据类型

''.join([])

>>> '* '.join(['flower','rain','sun'])
'flower* rain* sun'

b。元组

元组用 () 标识。内部元素用逗号隔开

>>> '.'.join(('a','b','c'))
'a.b.c'

c。集合{}

>>> '--'.join({'key1','key2','key3'})
'key1--key2--key3'

=========================================

2. python字符串的拼接  +

>>> a='hello'
>>> b='hhh'
>>> a + b
'hellohhh'
>>> a*2
'hellohello'
>>>

猜你喜欢

转载自www.cnblogs.com/lxyddm504/p/13177826.html