Python add item to the tuple

后端 未结 7 2094
暗喜
暗喜 2020-12-22 16:11

I have some object.ID-s which I try to store in the user session as tuple. When I add first one it works but tuple looks like (u\'2\',) but when I try to add ne

7条回答
  •  青春惊慌失措
    2020-12-22 17:02

    Bottom line, the easiest way to append to a tuple is to enclose the element being added with parentheses and a comma.

    t = ('a', 4, 'string')
    t = t + (5.0,)
    print(t)
    
    out: ('a', 4, 'string', 5.0)
    

提交回复
热议问题