python - Not enough arguments? -
print "%r"
vs
print "%r %r"%("hello")
i want compare above 2 lines of code in python.the latter statement gives error not enough arguements print in first case have no arguements still works.can explain new programming. appreciated.
the %
inside string comes play once python has established you're using %
printf-like operator outside string.
in first case, you're not, doesn't complain. you're doing in case printing string.
the second case, are using %
operator complains haven't provided enough arguments. in case, you're formatting string before printing it.
print
knows nothing of string formatting functions, prints give it. in string formatting case, "hello, %s"%("pax")
processed %
operator give "hello, pax"
, given print
output:
print "hello, %s"%("pax") # ^ \_________________/ <- bit done first # | # print done
so basically, it's %
operator decides you're doing printf-style processing, not fact have string containing %
character. that's confirmed fact that:
print "%r %r %r %r %r"
also has no issue argument count mismatch, printing out literal %r %r %r %r %r
.
Comments
Post a Comment