+ 1
I don't understand what is the backlash for?
What did backslash do in this program? print("He said: \"cool!\".")
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
+ 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 "..."
+ 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 \
+ 3
🥰 Thanks! I got it.