How do I replace slash character "'' by backslash character "/" in python

Question:

I need to replace forward slash "" with backward slash "/".

I have tried:

c = "/r/tes/integrtest_Ame.txt"
x = c.replace("", "/")
print(x)

But I have got the error:

File "C:Usersbwar1korDesktopTesttest.py", line 33
    x = c.replace("", "/")
                               ^
SyntaxError: EOL while scanning string literal
Asked By: jhonD

||

Answers:

” is an escape character, to use the actual character itself you need to double it:

c = r"/r/tes/integrtest_Ame.txt"
x = c.replace("\", "/")
print(x)
Answered By: SiP
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.