Printing tuple with string formatting in Python

前端 未结 14 2433
情话喂你
情话喂你 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 04:06

    This doesn't use string formatting, but you should be able to do:

    print 'this is a tuple ', (1, 2, 3)
    

    If you really want to use string formatting:

    print 'this is a tuple %s' % str((1, 2, 3))
    # or
    print 'this is a tuple %s' % ((1, 2, 3),)
    

    Note, this assumes you are using a Python version earlier than 3.0.

提交回复
热议问题