+ 1

I don't understand what is the backlash for?

What did backslash do in this program? print("He said: \"cool!\".")

9th Oct 2021, 4:02 PM
عبدالله أزهر
عبدالله أزهر - avatar
4 Answers
+ 8
The backslash is to let compiler know that quotation marks are part of the string, otherwise compiler will think it is the end of the string. Generally, backslashes are used for escape sequences in strings. These are the common escape sequences: \a - Bell(beep) \b - Backspace \f - Formfeed \n - New line \r - Carriage Return \t - Horizontal Tab \\ - Backslash \' - Single Quotation Mark \" - Double Quatation Mark \ooo - Octal Representation \xdd - Hexadecimal Representaion
9th Oct 2021, 4:10 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 6
It is called an "escape character" and allows you to use character that would otherwise be reserved for syntax Example: print("Say "hello"") # this would give an error print("Say \"hello\"") # this works and allows you too you " in a string that is declared with "..."
9th Oct 2021, 4:11 PM
Lisa
Lisa - avatar
+ 5
Hi! Backslash is commonly used as escape character. The compiler gives you an error if you use more than one set of quotes (" ") inside a print statement. But if you want to include a quote in a string, you need to escape it using a backslash. Output: He said: "cool!". Here quotes are escaped using backslash \
9th Oct 2021, 4:14 PM
Python Learner
Python Learner - avatar
+ 3
🥰 Thanks! I got it.
9th Oct 2021, 5:11 PM
عبدالله أزهر
عبدالله أزهر - avatar