Python: Converting from Tuple to String?

前端 未结 3 1751
情话喂你
情话喂你 2020-12-19 02:42

let\'s say that I have string:

    s = \"Tuple: \"

and Tuple (stored in a variable named tup):

    (2, a, 5)
相关标签:
3条回答
  • 2020-12-19 02:46

    Try joining the tuple. We need to use map(str, tup) as some of your values are integers, and join only accepts strings.

    s += "(" + ', '.join(map(str,tup)) + ")"
    
    0 讨论(0)
  • 2020-12-19 02:48

    This also works:

    >>> s = "Tuple: " + str(tup)
    >>> s
    "Tuple: (2, 'a', 5)"
    
    0 讨论(0)
  • 2020-12-19 02:58
    >>> tup = (2, "a", 5)
    >>> s = "Tuple: {}".format(tup)
    >>> s
    "Tuple: (2, 'a', 5)"
    
    0 讨论(0)
提交回复
热议问题