Printing tuple with string formatting in Python

前端 未结 14 2337
情话喂你
情话喂你 2020-11-28 03:19

So, i have this problem. I got tuple (1,2,3) which i should print with string formatting. eg.

tup = (1,2,3)
print \"this is a tuple %something\" % (tup)
         


        
14条回答
  •  有刺的猬
    2020-11-28 03:57

    >>> tup = (1, 2, 3)
    >>> print "Here it is: %s" % (tup,)
    Here it is: (1, 2, 3)
    >>>
    

    Note that (tup,) is a tuple containing a tuple. The outer tuple is the argument to the % operator. The inner tuple is its content, which is actually printed.

    (tup) is an expression in brackets, which when evaluated results in tup.

    (tup,) with the trailing comma is a tuple, which contains tup as is only member.

提交回复
热议问题