How to print a single backslash?

You need to escape your backslash by preceding it with, yes, another backslash: print(“\\”) And for versions prior to Python 3: print “\\” The \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, … Read more

Why do backslashes appear twice?

What you are seeing is the representation of my_string created by its __repr__() method. If you print it, you can see that you’ve actually got single backslashes, just as you intended: >>> print(my_string) why\does\it\happen? The string below has three characters in it, not four: >>> ‘a\\b’ ‘a\\b’ >>> len(‘a\\b’) 3 You can get the standard … Read more