-1

So I have a function that takes a string, iterates over a set of characters and returns a string with a backslash added to all the occurences of any character in that particular string:

def re_escape(string):
    res = "|\^&+\-%*/=!>"
    for i in res:
        if i in string:
            a = string.split(i)
            adjusted = ""
            for y in a:
                adjusted+="\\"+i+y
            adjusted = adjusted[2:]
            string = adjusted
    print(string)
    return string

Giving this function the string " <br>" returns " <br\>", as desired. However, going back to the part of the program calling this function and receiving the string as a return value, trying to print it results in " <br\\>" being printed. Is there any way to prevent it from adding the second undesired backslash?

vuv
  • 1
  • 2

1 Answers1

0

Give it a try: string.replace('\\\\','\\').

desertnaut
  • 57,590
  • 26
  • 140
  • 166
TheRagnar1
  • 61
  • 3