Converting a string to a tuple in python

后端 未结 3 757
佛祖请我去吃肉
佛祖请我去吃肉 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:54

    Use ast.literal_eval():

    >>> import ast
    >>> tc='(107, 189)'
    >>> tc_tuple = ast.literal_eval(tc)
    >>> tc_tuple
    (107, 189)
    >>> tc_tuple[0]
    107
    

提交回复
热议问题