繁体   English   中英

如何在 Python 中将 2 个打印语句的输出合并为一行?

[英]How to I combine the output of 2 print statements to a single line in Python?

这是代码

import sys

n=int(sys.argv[1])

first = 0
nexter = 0
second = 1

print(first,second)


for i in range(0,n-2):
    nexter = first+second
    print(nexter,end=" ")
    first=second
    second=nexter

输出是

0 1
1 2 3

但是,我想要的输出是

0 1 1 2 3

我该怎么做?

因此,您已经在print(nexter,end=" ")处组合了多个打印件。 这意味着以空格结束打印。 您执行与print(first,second ,end=" ") 完整代码:

import sys

n=int(sys.argv[1])

first = 0
nexter = 0
second = 1

# Only make this change of ending the print with " ". The default is a new line character.
print(first,second ,end=" ")


for i in range(0,n-2):
    nexter = first+second
    print(nexter ,end=" ")
    first=second
    second=nexter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM