removing space from output Python 2.7

百般思念 提交于 2019-12-20 06:29:33

问题


elif clocked > limit and clocked <= 90:
    print "Too fast! The fine is $", (clocked - limit) * 5 + 50
else:
    print "Thats was extremely dangerous!\nYour fine is $", \
                                 (clocked - limit) * 5 + 250

Hi, the output for the elif would like: Too fast! The fine is $ 50. I obviously want to get rid of the space between $ 50. I know I can introduce a new variable to assign my expression and then format the variable created with no spacing but is there any way around to skip adding new variable to the code? thanks.


回答1:


You don't have to introduce a new variable to use string formatting.

print "Too fast! The fine is ${0}".format((clocked - limit) * 5 + 50)



回答2:


Just change to use string interpolation (although if you're using 2.6+ - it won't hurt to use str.format intead as pointed out by @bobince)

print "Too fast! The fine is $%d" % ((clocked - limit) * 5 + 50,)


来源:https://stackoverflow.com/questions/14660744/removing-space-from-output-python-2-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!