How can I strip first and last double quotes?

后端 未结 13 1233
抹茶落季
抹茶落季 2020-12-02 10:14

I want to strip double quotes from:

string = \'\"\" \" \" \"\"\\\\1\" \" \"\" \"\"\'

to obtain:

string = \'\" \" \" \"\"\\\         


        
13条回答
  •  有刺的猬
    2020-12-02 11:04

    If you can't assume that all the strings you process have double quotes you can use something like this:

    if string.startswith('"') and string.endswith('"'):
        string = string[1:-1]
    

    Edit:

    I'm sure that you just used string as the variable name for exemplification here and in your real code it has a useful name, but I feel obliged to warn you that there is a module named string in the standard libraries. It's not loaded automatically, but if you ever use import string make sure your variable doesn't eclipse it.

提交回复
热议问题