+ 1
What's the purpose of backslashes?
I need a dumbed down explainantion
2 Respostas
+ 5
Ok so strings start with " and end with ".
Now how do you make a string that contains a ", without python getting horribly confused about where the string starts and ends?
Answer: You escape it with a backslash.
x = "Max said: \"I need water\""
Then of course, what if you need a string containing a \, without python thinking that you are escaping the next character? Easy: You escape it with a backslash.
x = "Heres a single backslash: \\"
The backslashes aren't actually part of the string, they are just a language construct so we can get rid of these ambiguities.
+ 4
Another common use is when you want to print newline characters, maybe extra blank lines between your output text, you can do eg.
print('This is the end. \n')
Then \n means an extra newline