Why does my code-
"She said \"Don't\" panic"
give the result-
'She said "don\'t" panic'
Why is a slash inserted after n in the result? How can i prevent it?
I'm going to assume you're running this in the interpreter. By default, the interpreter uses an object's __repr__() method when printing. However, print() uses __str__().
>>> s = "She said \"Don't\" panic"
>>> s
'She said "Don\'t" panic'
>>> print(s)
She said "Don't" panic
>>> print(s.__str__())
She said "Don't" panic
>>> print(s.__repr__())
'She said "Don\'t" panic'
The __repr__() method will return the string wrapped in '. To differentiate those ' from any within the string, it escapes those inner '.
This has the fun result of
>>> s.__str__()
'she said "Don\'t" panic'
Because it's printing __repr__("She said \"Don't\" panic"), which will wrap that string in ' and escape the inner ones.
When running the string provided above in a python shell, it prints out the raw string returned from the standard __repr__() function which contains the escape character required to include the single quote inside the string.
>>> "She said \"Don't\" panic"
'She said "Don\'t" panic'
You can simply wrap this inside of a print statement, which uses __str__(), to get the returned string you are looking for
>>> print("She said \"Don't\" panic")
She said "Don't" panic
The \t escape character in Python is a Horizontal Tab escape character.
If you're trying to sub-quote the "Don't Panic" within the overall quote, it would be:
"She said 'Don\'t panic'"
\t in his example. It's \'t. If you are using double quotes to surround your string, then you dont need to escape and single quotes inside the string either. Also even if you were, why would you escape one but not the other?