如何在同一行上打印2个项目

时间:2014-06-27 22:04:26

标签: python formatting output python-2.x

我希望打印新余额的2行是在同一行而不是2行。我该怎么做?

def main():

    # Initial balance
    balance = input ('Wallet balance: ')

    # Withdraw amount from wallet balance
    withdraw = input ('Withdraw amount: ')

    # Withdraw process and new balance display
    if withdraw > 0:
        new = balance - withdraw
        print ('Your new balance: ')
        print new

main()

1 个答案:

答案 0 :(得分:4)

用逗号分隔值:

print 'Your new balance:', new

参见下面的演示:

>>> new = 123
>>> print 'Your new balance:', new
Your new balance: 123
>>>
>>> print 'a', 'b', 'c', 'd'
a b c d
>>>

请注意,这样做会自动在值之间放置一个空格。