2.14 合并拼接字符串
问题
解决方案
>>> parts = ['Is', 'Chicago', 'Not', 'Chicago?']
>>> ' '.join(parts)
'Is Chicago Not Chicago?'>>> a = 'Hello' 'World'讨论
s = ''
for p in parts:
s += pLast updated
>>> parts = ['Is', 'Chicago', 'Not', 'Chicago?']
>>> ' '.join(parts)
'Is Chicago Not Chicago?'>>> a = 'Hello' 'World's = ''
for p in parts:
s += pLast updated
>>> data = ['ACME', 50, 91.1]
>>> ','.join(str(d) for d in data)print(a + ':' + b + ':' + c) # Ugly
print(':'.join([a, b, c])) # Still ugly
print(a, b, c, sep=':') # Better# Version 1 (string concatenation)
f.write(chunk1 + chunk2)
# Version 2 (separate I/O operations)
f.write(chunk1)
f.write(chunk2)def sample():
yield 'Is'
yield 'Chicago'
yield 'Not'
yield 'Chicago?'