什么是Python中的pythonic?

时间:2014-03-05 20:20:41

标签: python string python-2.7 coding-style string-formatting

python中哪个字符串实现更pythonic?

string += "<%s>%s</%s>%s" % (a, b, c, d)

string += '<' + a + '>' + b + '<'/' + c + '>' + d

1 个答案:

答案 0 :(得分:8)

字符串格式化更快,更易读。

考虑使用命名参数,以及更新的str.format() method

"<{tag}>{value}</{tag}>{tail}".format(tag='foo', value='bar', tail='spam')