How can I strip first and last double quotes?

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

I want to strip double quotes from:

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

to obtain:

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


        
13条回答
  •  情深已故
    2020-12-02 11:05

    To remove the first and last characters, and in each case do the removal only if the character in question is a double quote:

    import re
    
    s = re.sub(r'^"|"$', '', s)
    

    Note that the RE pattern is different than the one you had given, and the operation is sub ("substitute") with an empty replacement string (strip is a string method but does something pretty different from your requirements, as other answers have indicated).

提交回复
热议问题