converting string to tuple in python

前端 未结 5 1235
我寻月下人不归
我寻月下人不归 2021-01-22 02:12

I have a string returnd from a software like \"(\'mono\')\" from that I needed to convert string to tuple .

that I was thinking using ast.literal_eval

5条回答
  •  情深已故
    2021-01-22 03:04

    Try to this

    a = ('mono')
    print tuple(a)      # <-- you create a tuple from a sequence 
                        #(which is a string)
    print tuple([a])    # <-- you create a tuple from a sequence 
                        #(which is a list containing a string)
    print tuple(list(a))# <-- you create a tuple from a sequence 
                        #     (which you create from a string)
    print (a,)# <-- you create a tuple containing the string
    print (a)
    

    Output :

    ('m', 'o', 'n', 'o')
    ('mono',)
    ('m', 'o', 'n', 'o')
    ('mono',)
    mono
    

提交回复
热议问题