Why do backslashes appear twice?
Asked Answered
S

2

91

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

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

Why?

Sunlit answered 6/6, 2014 at 15:36 Comment(3)
If you genuinely have backslashes you want to remove, see #12618530Nonsuch
Also arguably related: stackoverflow.com/questions/1436703Hogtie
See Why does printing a tuple (list, dict...) in Python double the backslashes? for the specific issue where the repr of the string is used when printing a container type (dict/list/tuple) that contains a string.Hogtie
S
121

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.

Sunlit answered 6/6, 2014 at 15:36 Comment(3)
I've tried to keep this answer focussed specifically on the "double backslash" misunderstanding, but it's also useful to know about raw string literals.Sunlit
Input: 'I am 5' 6' output: ''I am 5\' 6'' I am trying so hard to find easy ways to achieve this but does not seems so easy. Any advice please?Citizen
It's not practical to answer a different question in comments, but careful reading of docs.python.org/3/tutorial/introduction.html#strings might help. Note that in order to unambiguously represent a string literal which contains both single and double quotes, Python is forced to escape at least one of those with a backslash. Note also that as explained above, the representation of a string in e.g. an interactive console is not the same as the content of that string.Sunlit
G
15

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 causes a DeprecationWarning, although most people don't see those. As of 3.12, it causes a SyntaxWarning. This will become a SyntaxError in some future version, per the docs on escape sequences.

$ python3.6 -Wall -c 'print("\d")'
<string>:1: DeprecationWarning: invalid escape sequence \d
\d
$ python3.12 -c 'print("\d")'
<string>:1: SyntaxWarning: invalid escape sequence '\d'
\d

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.

Gunnar answered 21/8, 2018 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.