2015-04-06 77 views
0

有什么conciser方式来表达:Python的格式输出

'{:f};{:f};{:f};{:f};{:f}'.format(3.14, 1.14, 2.14, 5.61, 9.80) 

这样一个并不需要写{:F}多次?

回答

2

你可以使用你能想到的任何好的方式来生成字符串,例如使用join

';'.join(['{:f}' for _ in range(5)]).format(3.14, 1.14, 2.14, 5.61, 9.80) 

下面是与列表理解里面的格式另一种变体。这很好,因为它不需要输入列表的长度。

nums = [3.14, 1.14, 2.14, 5.61, 9.80] 
';'.join(['{:f}'.format(n) for n in nums]) 
4

由无花果的回答启发(upvoted):

('{:f};'*5).format(3.14, 1.14, 2.14, 5.61, 9.80)[:-1] # strip the trailing semicolon