Why do backslashes appear twice while printing in Python?



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'

OUTPUT

"hello \ John's friends"

Advertisements