我使用python 2.6并阅读了许多关于从'print'中删除新行的链接,但是找不到使用示例以及使用模数符号(%)进行格式化.在我的程序中,我试图在计算数据的循环线中写入,但每个行数据来自不同的计算:
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()%()"语法删除换行符?
如果,
在语句末尾添加逗号(),则会省略换行符:
print ('%10d %10s') % (value1, value2),
Run Code Online (Sandbox Code Playgroud)
来自http://docs.python.org/reference/simple_stmts.html#print:
'\n'
除非