Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not enough arguments?

Tags:

python

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.

like image 862
user2688772 Avatar asked Nov 29 '22 12:11

user2688772


1 Answers

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.

like image 158
paxdiablo Avatar answered Dec 06 '22 20:12

paxdiablo