4
print "%r" 

vs

print "%r %r"%("hello") 

I want to compare above two lines of code in python.The latter statement gives an error with not enough arguements to print but in first case we have no arguements but still it works.Can anybody explain this to someone who is new to programming. Any help would be greatly appreciated.

2
  • look at @paxdiablo answer, print "%r %r" also works no problem Commented Aug 16, 2013 at 8:54
  • Actually, I'm going to vote to reopen this. Other than a similar title, the question itself is nowhere near a duplicate. The other one was about why a specific bit of code was failing, this one is about why the argument count problem doesn't occur for non-%-operator prints. Commented Aug 18, 2013 at 22:26

5 Answers 5

12

The % inside the string only comes into play once Python has established you're using the % printf-like operator outside the string.

In the first case, you're not, so it doesn't complain. All you're doing in that case is printing a string.

The second case, you are using the % operator so it complains that you haven't provided enough arguments. In that case, you're formatting a string before printing it.

print itself knows nothing of the string formatting functions, it simply prints what you give it. In the string formatting case, something like "Hello, %s"%("Pax") is processed by the % operator to give you "Hello, Pax" and then that is given to print for output:

print "Hello, %s"%("Pax") # ^ \_________________/ <- This bit done first # | # Then the print is done 

So basically, it's the % operator that decides you're doing printf-style processing, not the fact you have a string containing the % character. That's confirmed by the fact that:

print "%r %r %r %r %r" 

also has no issue with argument count mismatch, printing out the literal %r %r %r %r %r.

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

1 Comment

Note: if you're using printf-style processing and actually want to print a literal % character, you need to write %%.
3

print "%r" just prints the string %r.

print "%r %r" % ("hello"). % after the string %r %r is called a formatting operator and, because of it, %r here have a special meaning - it's a placeholder for the something you want to put on it's place.

You have two %r in the string - you should have two strings to put there:

print "%r %r" % ("hello", "world") 

Also see docs on string formatting.

Comments

3

You're not reading the error message:

>>> print "%r %r"%("hello") Traceback (most recent call last): File "<pyshell>", line 1, in <module> print "%r %r"%("hello") TypeError: not enough arguments for format string 

It's the format string which doesn't have enough arguments, not the print. This shows the same problem:

>>> "%r %r"%("hello") Traceback (most recent call last): File "<pyshell>", line 1, in <module> "%r %r"%("hello") TypeError: not enough arguments for format string 

Whereas this works:

>>> "%r %r"%("hello", "world") "hello world" 

Comments

3

It's worth noting that the only argument you are giving to print in all cases is the string. The Python interpreter evaluates any string formatting before print is called.

As @paxdiablo says, when you don't give any arguments to the %r format specifier, Python assumes you want the string as is, and does not complain.

Comments

0

The second line demands two arguments - there are two "%r", so you need two arguments in the parenthesis, like this:

print "%r %r" % ("hello", "world") 

Refer to the documentation.

1 Comment

This is hardly an answer to OP's question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.