How to remove duplicates of a single character in string python?

I have a string like this:


But what i want is:


Is there a way to do that?


  • First of all, if this is a string literal, then it will properly escape the slash, and you will get the result you want:


    Otherwise, you could just replace the characters with one of it:

    string = "$$\\int_{-\\infty}^{\\infty}\\int_{-\\infty}^{\\infty}{{e^{(x+y)}}^2}dxdy$$"
    string = string.replace(r'\\', '\\')