0

A linguistic question I guess, but what's the (5) called in the code print "%d" % (5) ?

I call the %d an integer representation, but I'm not sure what to call the stuff it actually represents, regardless of it being a number, a variable, a calculation or w/e. Is it called an argument?

I'm wondering because I'm making comments for an assignment where I'm calculating stuff in the parenthesis instead of making a new variable, calculating the variable and inserting the variable like x = 5;print "%d" % (x)

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
Dune
  • 5
  • 1
  • 3
    [The documentation](https://docs.python.org/2/library/stdtypes.html#string-formatting-operations) describes it as *"values"*, or *"the argument"*, and the `%d` a *"conversion specification"*. – jonrsharpe Sep 04 '14 at 12:59

2 Answers2

2

The documentation calls it "values":

If format requires a single argument, values may be a single non-tuple object.

The %d is called "conversion specifications". Each of them takes one or more "arguments" from the elements in "values".

%d takes one argument, %*d takes two, for example. All arguments make up "values".

That would mean 5 is the argument to %d while (5) is the "values" for the whole format.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 1
    *"So "argument" refers to the `%d`"* - no, that's the "conversion specification", the value (or tuple of values) is the argument. – jonrsharpe Sep 04 '14 at 13:06
0

(5) is actually a very misleading thing: it looks like it is a tuple, but it is merely an integer with parens around it!

Either write

print "%d" % 5

or the right way to write a single-element tuple,

print "%d" % (5,)

Or you will have problems later when you have a case where it does matter (like when instead of 5, you have a variable that may or may not be a tuple itself).

E.g.,

a = (2, 3)

print "%s" % a

raises TypeError, since a has two elements and there's only one %.

print "%s" % (a)

also raises TypeError, because it's the exact same thing, and

print "%s" % (a,)

actually prints the intended "(2, 3)".

RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79