-1

I have a string and I would like to add a \ before some characters.

I have my string in terms_string and what I do is:

charmap = {':', '(', ')', '{', '}', '[', ']', '<', '>', '/', '\\', '='}
for key in charmap:
    terms_string = terms_string.replace(key, '\\'+key)

Everything works fine but I get a \\ instead a \.

For example for the charmap I get

{'(', ')', '/', ':', '<', '=', '>', '[', '\\', ']', '{', '}'}

insted

{'(', ')', '/', ':', '<', '=', '>', '[', '\', ']', '{', '}'}

This only happens with the character \, not with the others.

Any idea why this is happening? Python configuration maybe? Thanks!

Oscar
  • 407
  • 6
  • 16
  • 1
    You can go check this https://stackoverflow.com/questions/24085680/why-do-backslashes-appear-twice . Your answer is here :) – Hanggy May 17 '19 at 07:08
  • If I do print(terms_string) I get everything with double \, so I think there is a problem. – Oscar May 17 '19 at 07:11
  • What do you want to achieve when trying to replace \\? You want to replace a single \ with \\ or you want to replace \\ with \\\ ? – crash May 17 '19 at 07:29
  • 1
    The **representation** as returned by `repr()`, in the console or in a sequence structure shows the escape sequence with two backslashes. But that is actually only one. If you print the singular string it will show it right. – Klaus D. May 17 '19 at 07:59

2 Answers2

1

I'm not sure what's your goal when replacing \ but these options may work for you.

terms_string = "foo (bar fo/o: b\a=r)"
charmap = {':', '(', ')', '{', '}', '[', ']', '<', '>', '/', '\\', '='}
for key in charmap:
    terms_string = terms_string.replace(key, "\\{}".format(key))
print(terms_string)
>>> foo \(bar fo\/o\: b\=r\)

or

import re
terms_string = "foo (bar fo/o: b\a=r)"
result = re.sub(r"([:\(\)\{\}<>/\\=])", r"\\\1", terms_string)
print(result)
>>> foo \(bar fo\/o\: b\=r\)

Also consider re.escape

terms_string = "foo (bar fo/o: b\a=r)"
print(re.escape(terms_string))
>>> foo\ \(bar\ fo/o:\ b=r\)
crash
  • 4,152
  • 6
  • 33
  • 54
  • I want to add \ before special characters because after that I want to use that string as a regular expression. I really liked re.escape, but before the spaces it adds a \ instead substitute the space by \s and I think that could be a problem – Oscar May 17 '19 at 12:11
  • if you want to use those strings in regular expressions, then `re.escape()` is actually what you need to use. I didn't understand the last part of your comment thou – crash May 17 '19 at 14:42
  • Normally what I always do in regex is \s for a space. Isn't that the normal thing to do? – Oscar May 18 '19 at 08:48
  • I am not extremely sure, but maybe `\s` is useful mainly when you want to capture multiple spaces with something like `\s+` ? – crash May 18 '19 at 08:50
  • Here they explain it very well (https://stackoverflow.com/questions/47763198/difference-between-using-s-and-an-actual-space-in-regex). \s works for any whitespace character (spaces, tabs, newlines...). So I guess in this case a simple space would work. – Oscar May 18 '19 at 08:57
  • Thanks for sharing the link, you're definitely right! – crash May 18 '19 at 09:15
0

This happens because \ is an escape character. If you give a single \ it looks for the character next to it to understand what this means. So for a literal \ it puts a \\ which does not then escape the \. Escape characters are characters with special meanings. For example '\n' means new line. Not a literal \n. Hope this clarifies your doubt. This happens not just in python but most languages.

Further reading

Raj Kumar
  • 1,547
  • 14
  • 19