Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do backslashes appear twice?

When I create a string containing backslashes, they get duplicated:

>>> my_string = "why\does\it\happen?" >>> my_string 'why\\does\\it\\happen?' 

Why?

like image 447
Zero Piraeus Avatar asked Jun 06 '14 15:06

Zero Piraeus


People also ask

Why do you have to put \\ in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. Conversely, prefixing a special character with "\" turns it into an ordinary character.

How do you escape a double slash?

Escape Required for Slashes As a corollary of the previous, you need to escape the escape character to avoid escaping the subsequent character. For example, '\' says to escape the second quote mark, leaving you with an unterminated string. However, '\\' results in a literal slash within the string.

When should you escape a backslash?

In JavaScript, the backslash is used to escape special characters, such as newlines ( \n ). If you want to use a literal backslash, a double backslash has to be used. So, if you want to match two backslashes, four backslashes has to be used. For example, alert("\\\\") will show a dialog containing two backslashes.


2 Answers

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 representation of a string (or any other object) with the repr() built-in function:

>>> print(repr(my_string)) 'why\\does\\it\\happen?' 

Python represents backslashes in strings as \\ because the backslash is an escape character - for instance, \n represents a newline, and \t represents a tab.

This can sometimes get you into trouble:

>>> print("this\text\is\not\what\it\seems") this    ext\is ot\what\it\seems 

Because of this, there needs to be a way to tell Python you really want the two characters \n rather than a newline, and you do that by escaping the backslash itself, with another one:

>>> print("this\\text\is\what\you\\need") this\text\is\what\you\need 

When Python returns the representation of a string, it plays safe, escaping all backslashes (even if they wouldn't otherwise be part of an escape sequence), and that's what you're seeing. However, the string itself contains only single backslashes.

More information about Python's string literals can be found at: String and Bytes literals in the Python documentation.

like image 146
Zero Piraeus Avatar answered Sep 20 '22 14:09

Zero Piraeus


As Zero Piraeus's answer explains, using single backslashes like this (outside of raw string literals) is a bad idea.

But there's an additional problem: in the future, it will be an error to use an undefined escape sequence like \d, instead of meaning a literal backslash followed by a d. So, instead of just getting lucky that your string happened to use \d instead of \t so it did what you probably wanted, it will definitely not do what you want.

As of 3.6, it already raises a DeprecationWarning, although most people don't see those. It will become a SyntaxError in some future version.


In many other languages, including C, using a backslash that doesn't start an escape sequence means the backslash is ignored.

In a few languages, including Python, a backslash that doesn't start an escape sequence is a literal backslash.

In some languages, to avoid confusion about whether the language is C-like or Python-like, and to avoid the problem with \Foo working but \foo not working, a backslash that doesn't start an escape sequence is illegal.

like image 29
abarnert Avatar answered Sep 20 '22 14:09

abarnert