2014-01-29 58 views
0

如何改變,的默認值在下面的代碼,以便它不插入空格如何更改Python中逗號的值?

gas = int(input("How much have you spent on gas? ")) 
electric = int(input("How much have you spent on electric? ")) 
onlinePurchases = int(input("How much have you spent on online purchases? ")) 
total = gas + electric + onlinePurchases 

print("Monthly Total: $", total) 

當前的代碼將打印:

"Monthly total: $ *total*" 

有沒有一種方法,使它打印:

"Monthly total: $*total*" 

我不希望美元符號和金額之間的空間。

回答

4

從蟒蛇printdocs

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) 

所以,你想

print("Monthly Total: $", total, sep='') 

更改sep從空間(分離)爲空字符串

+0

謝謝你,只是讓我知道是「月」短期的「分離」,如果沒有這是什麼立場? – user3131132

+0

這是正確的 – mhlester

3

print("Monthly Total: ${0}".format(total))

print("Monthly Total: $%d" % total)