打印时有额外的空间

时间:2012-10-12 16:35:34

标签: python whitespace string-formatting

我已阅读了一些python空格删除问题和答案,但未能找到我正在寻找的内容。这是一个小程序,显示了该问题的具体示例。我非常感谢你的帮助。

import random

math_score = random.randint(200,800)
math_guess = int(input("\n\nWhat score do you think you earned on the math section (200 to 800)?\t"))
print ("\n\n\nOn the math section, you guessed",math_guess,", and your actual score was",math_score,"!")

所以这是我的问题:

当我执行程序时,我得到以下结果:

On the math section, you guessed 600 , and your actual score was 717 !

我想删除句子中每个变量后面的空格。在这种情况下,600和“,”之间的空间以及717和“!”之间的空间。

是否有解决此问题的标准方法?

4 个答案:

答案 0 :(得分:4)

是的,format你的字符串:

print("... you guessed {}, and ... was {}!".format(math_guess, math_score))

答案 1 :(得分:0)

您需要format整行代表一个字符串,然后打印该字符串。

print ("\n\n\nOn the math section, you guessed {0}, and your actual score was {1}!".format(math_guess, math_score))

答案 2 :(得分:0)

print ("\n\n\nOn the math section, you guessed",math_guess,", and your actual score was",math_score,"!", sep ='')

如果这是py3 +我认为

print ("\n\n\nOn the math section, you guessed"+str(math_guess)+", and your actual score was"+str(math_score)+"!")

如果不是

应该有效

或使用其他人建议的字符串格式...

答案 3 :(得分:0)

试试这个:

print "\n\n\nOn the math section, you guessed %d and your actual score was %d!" % (math_guess, math_score)

您可以在Built-in Types

了解更多信息