Why escaping exists
A Kotlin string literal starts and ends with a double quote. That makes the double quote a special character: inside a string, it means "the string stops here". So this line can't print a quoted phrase:
println("I am John "the coder" Doe")
The compiler reads "I am John " as a complete string, then sees the coder as code, and reports errors. The string was never meant to end there.
Escaping solves this. A backslash \ in front of a character tells Kotlin to treat that character in a special way. For a double quote, it means "this is an ordinary character, not the end of the string":
println("I am John \"the coder\" Doe")
println("After \"Coding 101\" we'll solve some problems")
This prints:
I am John "the coder" Doe
After "Coding 101" we'll solve some problems
The apostrophe in we'll needs no escape, because a single quote doesn't end a double-quoted string.
The backslash itself
The backslash is special too, since it starts every escape sequence. A Windows path such as C:\Users\Andy\Games contains backslashes that Kotlin would read as the start of escapes: \U, \A and \G. Kotlin has no escape sequence by those names, so the compiler reports unsupported escape sequence for each of them.
To get one real backslash, write two:
println("C:\\Users\\Andy\\Games")
C:\Users\Andy\Games
The pair \\ in the source code is a single character in the string. The string's length counts it as one.
Escapes that do something special
Escaping can work the other way too: a backslash can give an ordinary letter a special meaning. The most useful one is \n, which is a line break:
println("Hello world!\nWhat a good day for coding!\n")
println("Let's have a blast!")
Hello world!
What a good day for coding!
Let's have a blast!
The empty line comes from two line breaks in a row: the \n at the end of the string, and the one that println always adds after its argument.
Here are the escape sequences Kotlin supports in a regular string:
| Escape | Meaning |
|---|---|
\n | new line |
\t | tab |
\r | carriage return |
\b | backspace |
\" | double quote |
\' | single quote |
\\ | backslash |
\$ | dollar sign |
\uXXXX | a Unicode character, given by four hexadecimal digits |
Kotlin has no other escape sequences. The dollar sign is on the list because it starts a string template: "$n" inserts the value of the variable n. To print a literal dollar sign before a name, write "It costs \$5", which prints It costs $5.
Raw strings as an alternative
For text full of backslashes, Kotlin has raw strings, wrapped in three double quotes. Escapes aren't processed inside them, so a path can be written as it is:
println("""C:\Users\Andy""")
C:\Users\Andy
This lesson practises escape sequences in regular strings, so the exercise uses those.
The exercise, step by step
- Quotes. Print
I'll study "Coding 101" on AlgoCademy. Escape both inner double quotes as\". The apostrophe inI'llstays as it is. - Two lines in one call. With a single
println, printI will learn thisandNo matter whaton separate lines. Put\nbetween them inside one string. - The path. Print
C:\Users\Andy\notion. Double each backslash so that none starts an escape sequence. With single backslashes,\Uwould be a compile error, and even if the other backslashes were doubled, the\nin\notionwould become a line break instead of the letters.
println("I'll study \"Coding 101\" on AlgoCademy")
println("I will learn this\nNo matter what")
println("C:\\Users\\Andy\\notion")
The output:
I'll study "Coding 101" on AlgoCademy
I will learn this
No matter what
C:\Users\Andy\notion
Common mistakes
- Forgetting to escape an inner quote. The string ends early and the rest becomes code that doesn't compile.
- A single backslash in a path. It either fails to compile, as with
\U, or silently changes the text, as with\nor\t, which turn into a line break or a tab. - Using a forward slash for a backslash.
/nis just two characters. The escape needs a backslash. - Putting a space after the backslash.
\ nis not an escape sequence. - Adding
\nto aprintlnand expecting no extra blank line.printlnalready ends its output with a line break.
Key takeaways
- A backslash starts an escape sequence inside a string.
\"prints a double quote,\\prints a backslash and\nstarts a new line.- A backslash before a character that Kotlin doesn't recognise is a compile error.
- Raw strings with three double quotes skip escape processing.