1

How would I remove the spaces after the R in the following line?

>>>print "4/3 =", 4 / 3, "R", 4 % 3

I want the result to look exactly like this:

4/3 = 1 R1

I appreciate any time anyone takes to answer this question.

1
  • Read about string formatting. Commented Apr 25, 2012 at 6:32

3 Answers 3

7

Use str.format:

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

If you are using an older version of Python which doesn't support str.format then you can use the % operator:

>>> print "%d/%d = %d R%d"%(a, b, a//b, a%b)
4/3 = 1 R1

Note also that since Python 2.2 you should use // for integer division, and from Python 3 onwards, you must use //.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use formatting.

print "4/3 = %d R%d" % (4 / 3, 4 % 3)

or even better, with string.format:

print "4/3 = {} R{}".format(4 / 3, 4 % 3)

Comments

0

You should use string format

print "4/3 =%d R%d" % (4 / 3, 4 % 3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.