1

I'm studying the code of an open source project. The code it's written in python and unfortunately I'm not so experienced with it. I found a lot of statement in the code like this:

print "the string is %s" % (name_of_variable, )

I know that, similarly to c language, this is a way to properly format the output, but I really don't understand what is the meaning of the comma right after the "name_of_variable" within the parenthesis.

I searched through python documentation, but I didn't find nothing about that kind of statement. Does anyone know what is its meaning?

user3098549
  • 1,171
  • 2
  • 13
  • 26

7 Answers7

3

The trailing comma is a way to create a one-element tuple. Normally you create a tuple by listing a set of values or variables in parentheses, separated by a comma:

my_tuple = (1, 2, 3)

However, the parentheses are not actually necessary. You can create a tuple like this:

my_tuple = 1, 2, 3

Of course, this raises an issue with one-element tuples. If you leave out the parentheses, you're just assigning a variable to a single variable:

my_tuple = 1    # my_tuple is the integer 1, not a one-element tuple!

But using parentheses is ambiguous, since surrounding a value in parentheses is totally legal:

my_tuple = (1)    # my_tuple is still the integer 1, not a one-element tuple

So you indicate a one-element tuple by adding a trailing comma:

my_tuple = 1,
# or, with parentheses
my_tuple = (1,)

String formatting takes a tuple as an arguments, so if you're using a one-element tuple, you'd use a trailing comma. (Of course, string formatting has special handling for cases in which you are passing only one variable to be formatted—you can just use a value in those cases, instead of a tuple.)

mipadi
  • 398,885
  • 90
  • 523
  • 479
3

If name_of_variable is a tuple, then

print "the string is %s" % (name_of_variable)

will throw a TypeError.

As a result you'll have to use print "the string is %s" % (name_of_variable, ) to print it.

Dhruv
  • 1,079
  • 2
  • 13
  • 26
1

The comma is used when you have more than one variables. eg

print "the string is %s %s" % (name_of_variable1, name_of_variable2)

More details are here https://stackoverflow.com/a/5082482/2382792

Community
  • 1
  • 1
ρss
  • 5,115
  • 8
  • 43
  • 73
0

It doesn't matter whether you have comma or not when you are using only one variable, you also don't need the parentheses with only one variable.

It uses tuple (parentheses and comma) here just to be consistent with the multiple variables situations.

And, if you don't have a comma in a one-element tuple, it's no longer a tuple.

For example, a = (1) type(a) # <type 'int'>

People also like to append an additional comma after the last element in a tuple even if they have more than one elements to keep consistency.

Rikka
  • 999
  • 8
  • 19
0

This is the syntax to create one-element tuple. See this link for example. It's not essential here, as print will also accept one variable, but in general (x) is equivalent to x, so the only way to force it to be interpreted as a tuple syntax is this weird-looking form. Note the difference:

>>> x = 0
>>> (x)
0
>>> (x,)
(0,)
>>> type((x))
<type 'int'>
>>> type((x,))
<type 'tuple'>
>>>

Also, the parentheses are not needed:

>>>x,
>>>(0,)

You may want to also read about %-formatting syntax.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
0

The comma in (x,) is to tell Python the parenthesis enclose a tuple, as opposed to a parenthesized form of a single value subexpression (most frequently used to change operator precedence, such as (a+b) * 2 being different from a + b*2). The string formatting operator is fairly good at guessing when a single value is not supposed to be a sequence, but if you were to pass a tuple instead of a string it would get unwrapped:

>>> mytup=("Hello world!",)
>>> print "%s"%(mytup)
Hello world!
>>> print "%s"%(mytup,)
('Hello world!',)
>>> mytup=("Hello world!","Yoo-hoo, big summer blow-out!")
>>> print "%s"%(mytup)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting

The operator tries to resemble a function call, where the argument list is always passed as a tuple (and possibly a dictionary for named arguments). This discrepancy disappears when instead called as str.format as the tuple-forming parenthesis are part of the method call syntax.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
-1

Here , is not specific as you are using only single variable.

In [96]: print "the string is %s" % (name_of_variable, )
the string is assdf

In [97]: print "the string is %s" % name_of_variable
the string is assdf

Output is same.

but if the name_of_variable is a tuple then it's necessary to use , in print statement

In [4]: tup = (1,2,)
In [6]:  print ("the string is %s" % (tup))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-fb9ef4de3ac3> in <module>()
----> 1 print ("the string is %s" % (tup))

TypeError: not all arguments converted during string formatting

In [7]:  print ("the string is %s" % (tup,))
the string is (1, 2)
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
  • You also don't need the parentheses. – Vincent Beltman Nov 11 '14 at 09:57
  • So, why do you think that who wrote the code used the commas? I've found it in a lot of statements, so it seems strange that it's a typo – user3098549 Nov 11 '14 at 09:59
  • @user3098549 It just a pythonic way to write. `,` parts when you are making tuple of sinlge item. `t = 1,` makes a tuple while `t = 1` just an integer. – Vishnu Upadhyay Nov 11 '14 at 10:01
  • Since a tuple is necessary for >1 arguments, there is a strong trend to use tuple for a single argument too; it is simply easier to extebd in case of future additional arguments. – guidot Nov 11 '14 at 10:20
  • @guidot yes, i knew it but thanks to make me correct here, but in question user didn't specify that input is a tuple, i am just trying to illustrate. – Vishnu Upadhyay Nov 11 '14 at 10:25