如果我有这样的程序:
def P(x):
# x is an integer
print str(x)
我想要一个输出,例如:
>>> You chose the number: X
其中 X 是过程 P 中打印的结果。 如何在不更改程序的情况下做到这一点?
如果我这样做:
print 'You chose the number: '
P(x)
我会得到
You chose the number:
X
如何让它们在同一行?
最佳答案
在第一个打印语句后添加一个trailing
逗号,以打印同一行中的下一个语句:-
print 'You chose the number: ',
P(x)
关于python - 在同一行格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13048337/