Printing tuple with string formatting in Python

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

    t = (1, 2, 3)
    
    # the comma (,) concatenates the strings and adds a space
    print "this is a tuple", (t)
    
    # format is the most flexible way to do string formatting
    print "this is a tuple {0}".format(t)
    
    # classic string formatting
    # I use it only when working with older Python versions
    print "this is a tuple %s" % repr(t)
    print "this is a tuple %s" % str(t)
    

提交回复
热议问题