What's the purpose of backslashes? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What's the purpose of backslashes?

I need a dumbed down explainantion

16th Jan 2019, 9:07 PM
Zeeshan
Zeeshan - avatar
2 Answers
+ 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.
16th Jan 2019, 9:11 PM
Schindlabua
Schindlabua - avatar
+ 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
16th Jan 2019, 9:23 PM
Tibor Santa
Tibor Santa - avatar