Python 字符串拼接

+ :

1 s1 = 'a'
2 s2 = 'b'
3 s3 = 'c'
4 s = 'I have ' + s1 +', ' + s2 + ', ' + s3
5 输出: 'I have a, b, c'

%:

1 s1 = 'a'
2 s2 = 'b'
3 s3 = 'c'
4 s = 'I have %s, %s, %s'%(s1, s2, s3)
5 输出: 'I have a, b, c'

.format:

1 s1 = 'a'
2 s2 = 'b'
3 s3 = 'c'
4 s = 'I have {}, {}, {}'.format(s1, s2, s3)
5 输出: 'I have a, b, c'

推荐使用 .format 和 %方式拼接字符串。

posted @ 2018-11-28 23:29  deep-thinking  阅读(168)  评论(0编辑  收藏  举报