vmp*_*vmp 2 python string formatting
如果我有一个程序,如:
def P(x):
# x is an integer
print str(x)
Run Code Online (Sandbox Code Playgroud)
我希望有一个输出,如:
>>> You chose the number: X
Run Code Online (Sandbox Code Playgroud)
其中X是在程序P中打印的结果.如何在不更改程序的情况下执行此操作?
如果我喜欢这样:
print 'You chose the number: '
P(x)
Run Code Online (Sandbox Code Playgroud)
我去拿
You chose the number:
X
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它们在同一条线上?
trailing在第一个print语句后添加逗号,以在同一行中打印下一个语句: -
print 'You chose the number: ',
P(x)
Run Code Online (Sandbox Code Playgroud)