Reputation: 3
In the interpreter, when I'm writing the code
"it's 42\""
To make python display
'it's 42"'
It's displaying
'it\'s 42"'
Instead. Now, I would like to understand why, more then getting around that.
Upvotes: 0
Views: 67
Reputation: 25895
When you type to the shell:
"it's 42\""
It displays the string variable created from what your wrote, and python defaults to ' usage. So you get:
'it\'s 42"'
which, when you think about it, is the exact same string you wrote! You used "" to denote the string and escaped the extra ", but python uses in the shell ' and escapes the extra one in the it ' s.
To show the string properly use the print method which reads the escapes properly. I guarantee printing your version or the version your shell returns will both yield what you wanted.
On a side note you can use triple quotes to skip escapes all together:
'''it's 42"'''
The caveat is you can't use triple double quotes, because python will think the end of your string is part of the string enclosure.
Upvotes: 0
Reputation: 504
I thin it's because if you use ' in string, for display it, python frame string with " :
>> "it's 42"
"it's 42"
Now if use " in string, python frame string with ' :
>> "\"it s 42\""
'"it s 42"'
But if you use 2 in the same time python escape automatically one of two for not confuse with that frames the chain :
>> "it's 42\""
'it\'s 42"'
Upvotes: 1
Reputation: 60964
Have you tried print("it's 42\"")
? If you are just doing
>>>"it's 42\""
'it\'s 42"
Then you are not actually printing the string, just displaying its value.
Upvotes: 1