Converting a string to a tuple in python

后端 未结 3 870
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-15 04:13

Okay, I have this string

tc=\'(107, 189)\'

and I need it to be a tuple, so I can call each number one at a time.

print(tc[         


        
3条回答
  •  清歌不尽
    2021-01-15 04:56

    You can use the builtin eval, which evaluates a Python expression:

    >>> tc = '(107, 189)'
    >>> tc = eval(tc)
    >>> tc
    (107, 189)
    >>> tc[0]
    107
    

提交回复
热议问题