I want to know how to use Python’s str.replace() function (or similar) to replace Backslashes…
When i try to do it:
>>> temp = r"abc\abc"
>>> temp.replace(r'\'', 'backslash')
'abc\\abc' # For some reason, temp.replace() does not replace '\' with 'backslash' even when using raw variable
>>> temp.replace(r'\\', 'backslash') # Same result
'abc\\abc'
How do i fix this? And why? (Linux, Debian/Ubuntu, x86_x64 processor)
>Solution :
You need to escape the backslash –
temp = r"abc\abc"
temp.replace('\\', 'backslash')
'abcbackslashabc'