Why strings need escaping
A Swift string literal starts and ends with a double quote. That makes the double quote a special character: inside a string, it normally means "the string ends here". So this line doesn't work:
print("I am John "the coder" Doe")
Swift reads the string "I am John ", then finds the word the where it expects the end of the statement, and reports errors. To put a double quote inside a string, you escape it with a backslash.
Escaping a double quote
A backslash in front of a character tells Swift to treat the next character in a special way. \" stands for one literal double quote that does not end the string:
print("I am John \"the coder\" Doe")
print("After \"Coding 101\" we'll solve some problems")
The output is:
I am John "the coder" Doe
After "Coding 101" we'll solve some problems
The apostrophe in we'll needs no escape. Only the character that closes the string, the double quote, is special inside a double-quoted string.
Escaping the backslash itself
The backslash is special too, so a string can't contain a lone one. A file path shows the problem:
print("C:\Users\Andy\Games")
Swift tries to read \U as an escape sequence, and there is no such sequence. The compiler stops with error: invalid escape sequence in literal. To get a real backslash, write two:
print("C:\\Users\\Andy\\Games")
This prints C:\Users\Andy\Games. In the source, every \\ counts as one character in the string.
The new line escape
Escapes also work the other way: a backslash can give a plain letter a special meaning. \n is a line break.
print("Hello world!\nWhat a good day for coding!\n")
print("Let's have a blast!")
The output has an empty line after the second sentence:
Hello world!
What a good day for coding!
Let's have a blast!
That empty line is the \n at the end of the first string plus the line break that print adds after any text.
Common escape sequences in Swift
| Write | You get |
|---|---|
\" | a double quote |
\\ | a backslash |
\n | a new line |
\t | a tab |
\' | an apostrophe (allowed, rarely needed) |
\u{41} | the Unicode character with that hex code, here A |
\(expression) | the value of the expression, inserted into the string |
The last row is string interpolation, which also begins with a backslash. If you want the characters \( as text, write \\(.
Ways to avoid escaping
Swift has two other forms for text full of quotes or backslashes. A raw string puts one or more # signs around the quotes, and inside it a backslash is plain text:
print(#"C:\Users\Andy"#)
This prints C:\Users\Andy. A multi-line string uses three quotes, and a single " inside it needs no escape:
print("""
She said "hi"
""")
This prints She said "hi". The lesson's exercise uses regular escapes, so these are for later.
The exercise
The exercise has three prints, one step each:
- Quotes. Print
I'll study "Coding 101" on AlgoCademy. The two double quotes aroundCoding 101get a backslash; the apostrophe doesn't. - Two lines from one print. Put
\nbetweenI will learn thisandNo matter what. The result is two lines from a singleprint. - A path. Print
C:\Users\Andy\notionwith each backslash doubled. Without doubling,\Uis an error, and\nin\notionwould silently become a line break.
print("I'll study \"Coding 101\" on AlgoCademy")
print("I will learn this\nNo matter what")
print("C:\\Users\\Andy\\notion")
Key takeaways
- A backslash starts an escape sequence inside a string literal.
- Use
\"for a double quote and\\for a backslash. \nis a new line, andprintadds one more line break after the whole text.- An unknown sequence such as
\Uis a compile error. - Raw strings (
#"..."#) and multi-line strings (""") reduce the need for escapes.
