Python: Merge concatenated strings join() | format() | +

Combine several small strings into one big string

 

1 If the merge is a sequence, the fastest way is to use the join() method

>>> parts = ['Is', 'Chicago', 'Not', 'Chicago?']
>>> ' '.join(parts)
'Is Chicago Not Chicago?'
>>> ','.join(parts)
'Is,Chicago,Not,Chicago?'
>>> ''.join(parts)
'IsChicagoNotChicago?'


 

 2 Just combine a few strings, use "+"

>>> a = 'Is Chicago'
>>> b = 'Not Chicago?'
>>> a + ' ' + b
'Is Chicago Not Chicago?'
>>>


3 simple put together

>>> a = 'Hello' 'World'
>>> a
'Hello


4format() function

>>> print('{} {}'.format(a,b))
Is Chicago Not Chicago?

 

5 or

print(a, b, c, sep=':')

 

6 Performance Issues

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326386249&siteId=291194637