thisdict = {"brand": "Ford", "model": "\\[vocalized-noise\\]", "year": 1964}
x = thisdict["model"]
print(x)
Output:
\[vocalized-noise\]
Expected Output:
\\[vocalized-noise\\]

- 26,043
- 3
- 22
- 34

- 13
- 3
5 Answers
"\\"
is a string literal that creates a string with one backslash. If you want your output to show two backslashes, there need to be two in the first place.
>>> x = "\\"
>>> print(x)
\
>>> x = "\\\\"
>>> print(x)
\\
From left to right, a backslash escapes the following character: the process causes the escaped character to be replaced by another character determined by the first; the backslash that did the escaping is removed as well. Some examples:
'\t'
-> a single tab character (t
for tab)'\r'
-> a single carriage return (r
for carriage return)'\\'
-> a single backslash (\
for backslash)
Applied to you dict literal, that would be
thisdict = {"brand": "Ford", "model": "\\\\[vocalized-noise\\\\]", "year": 1964}

- 497,756
- 71
- 530
- 681
-
1/me searches for a joke, fails, and admits it is a typo. – chepner Oct 23 '19 at 20:15
Backslash is the string escape character; to escape a backslash, you double it. So when you do '\\'
, you're saying "that second backslash isn't escaping the trailing single-quote, because the first backslash makes it a plain backslash (and the first backslash is otherwise not part of the string)".
If you want the backslashes to be interpreted literally, with no escaping (aside from the quote character itself, meaning you can't end a string with a backslash), use raw strings, prefixed with an r
, and all the backslashes will be interpreted as literal backslashes:
thisdict = {"brand": "Ford", "model": r"\\[vocalized-noise\\]", "year": 1964}
The only alternative is to double the backslashes, which gets ugly fast:
thisdict = {"brand": "Ford", "model": "\\\\[vocalized-noise\\\\]", "year": 1964}

- 143,180
- 12
- 188
- 271
-
1Just note for who is using `r` when you do `r'\\abc\\\'` it will throw an error. `r` will only work if there is no `\` right before ending quote – Poojan Oct 23 '19 at 20:24
-
1@Poojan: Yeah, that's addressed in the parenthetical in the second paragraph. – ShadowRanger Oct 23 '19 at 23:16
The backslash character \
is used to "escape" other special characters, like quote marks. It can, itself, be escaped to indicate to python that you literally mean the \
character, not some special escape sequence. If you type \\
, it will render as \
.
Escape your slashes when you insert the data as \\\\[vocalized-noise\\\\]
to have them print \\
normally.

- 4,989
- 4
- 17
- 37
- Easier way to do it. use
r''
.' \
is an escape character in python.- Using
r
you dont have to manually add\
before each\
.
>>> s = r'\\[vocalized-noise\\]'
>>> s
'\\\\[vocalized-noise\\\\]'
>>> thisdict = {"brand": "Ford", "model": r"\\[vocalized-noise\\]", "year": 1964}
>>> x = thisdict['model']
>>> print(x)
\\[vocalized-noise\\]
r
meansraw_string
. It will take\
as backslash except when it comes right before a quote that would otherwise terminate the literal.
ps: You can read more on r''
here What exactly do "u" and "r" string flags do, and what are raw string literals?

- 3,366
- 2
- 17
- 33
IF you need to escape data like this programmatically, you can do it like this:
>>> s = "\\[vocalized-noise\\]"
>>> escaped = s.encode('unicode-escape').decode('raw-unicode-escape')
>>> escaped
'\\\\[vocalized-noise\\\\]'
>>> print(escaped)
\\[vocalized-noise\\]
The unicode-escape codec will escape the backslashes, decoding from the raw-unicode-escape codec preserves them while otherwise decoding unicode-escape.

- 47,570
- 11
- 100
- 153