我使用python 2.6并阅读了很多关于从'print'中删除新行的链接,但找不到使用模数标志(%)格式化的用法示例。在我的计划,我想在计算数据的环行写的,但每一行数据来自不同的计算:python 2.6 print with formatting(%):remove newline
while loop ... calulating value1 and value2 print ('%10d %10s') % (value1, value2) [1] ... calulating value3 and value4 print ('%7s %15d') % (value3, value4) [2] print #this is where newline should come from
所以我想获得:
value1 value2 value3 value4 value5 value6 value7 value8 ...
基本上这个方法可以使可读性我的计划(每个真实行有超过20个计算位置)。相反的方式是将所有数据连接成一个长串,但可读性可能会丢失。
是否可以像[1]和[2]中那样使用“print()%()”语法来删除换行符?
请问你刚找到一份工作的方法,如果你使用[str.format(http://docs.python.org/library/stdtypes.html#str.format)而不是旧式%格式?我知道至少会有逗号,尽管我怀疑这会使用%格式。 (尽管如此,你仍然应该使用str.format) – Josiah 2012-08-07 09:46:16