0

How to use backslash on double quote

print('I\'m learning Python!') print("A number enclosed in double quotes: "42"")

11th Apr 2021, 6:35 PM
Travis Business
Travis Business - avatar
2 Réponses
+ 1
The backslash escape characters with special meanings. n becomes a newline \n, and quotes becomes a character without the ‘end the string’ meaning the quote normal gets inside a string, if the string began with the same kind of quote. So it becomes an ordinary character with a backslash \" . s0 = "\"" becomes a string with one character: len(s0) = 1 and print(s0) gives: " There is nothing special with “ (duoble qoute) comparing to ‘ (singel quote). So to enclose a number in quotes you can escape them: s1 = "\"42\"" or: s2 = '"42"' In the last one the string starts and ends with single qutoes, so the double quotes becomes ordinary characters inside the string.
11th Apr 2021, 7:32 PM
Per Bratthammar
Per Bratthammar - avatar
0
This line doesn't produce errors. It is okay as it is: print('I\'m learning Python!') This line was producing an error. print("A number enclosed in double quotes: "42"") The corrected syntax is as follows: print("A number enclosed in double quotes: \"42\"")
11th Apr 2021, 7:27 PM
EO4Wellness
EO4Wellness - avatar