Why do backslashes appear twice while printing in Python?

PythonServer Side ProgrammingProgramming

Backslash is an escape character. It is used to escape characters like quotes, double quotes, new line, etc. When we print a string containing a backslash, we see it twice because a backslash is also needed to escape itself. It means that we are telling the interpreter that this backslash is to be used as a backslash only and not as an escape character. For example,

print 'hello \ John\'s friends'


"hello \ John's friends"
Updated on 30-Sep-2019 07:00:20