如何在使用Python时阻止空格?

Ant*_*rez 1 python formatting spaces

如何删除以下行中R之后的空格?

>>>print "4/3 =", 4 / 3, "R", 4 % 3
Run Code Online (Sandbox Code Playgroud)

我希望结果看起来像这样:

4/3 = 1 R1

任何人都回答这个问题我都很感激.

Mar*_*ers 7

用途str.format:

>>> a=4
>>> b=3
>>> print "{}/{} = {} R{}".format(a, b, a//b, a%b)
4/3 = 1 R1
Run Code Online (Sandbox Code Playgroud)

如果您使用的是不支持的旧版Python,str.format则可以使用%运算符:

>>> print "%d/%d = %d R%d"%(a, b, a//b, a%b)
4/3 = 1 R1
Run Code Online (Sandbox Code Playgroud)

另请注意,从Python 2.2开始,您应该使用//整数除法,从Python 3开始,您必须使用//.