Just a further note on what has already been mentioned, each time you use a backslash it consumes the character following it. As you probably know, some have special meaning such as \t
would insert a tab character. As you've seen, \\
is designed to show the backslash.
What Python also lets you do is prefix any string with r
which is used to mean disable the escape mechanism inside the string. For example in your first example, adding the r
would display all three backslashes 'as is'.
print r"this is \\\ a string"
this is \\\ a string
But be warned, even this trick will fail if you try your second example. You still need to avoid a backslash in the last character of a string:
print r"\\\"
SyntaxError: EOL while scanning string literal