What a string is
A string is a piece of text: any sequence of characters you can type, such as letters, digits, spaces and symbols. In Swift you create one by putting the text inside double quotes, like "Hello world". The type is called String.
Strings are what you pass to print() when you want to show a message:
print("My name is Andy")
print("Welcome to the course!")
My name is Andy
Welcome to the course!
The quotes are not in the output. They are not part of the string: their only job is to tell Swift where the text starts and where it ends.
Strings are not numbers
The text "12" is a string of two characters, while 12 is a number. They print the same, but they behave differently:
print("12" + "3")
print(12 + 3)
123
15
On strings, + joins the pieces together. On numbers, it adds them.
Useful things to know
Inserting values
Inside a string, \( ) places the value of an expression into the text. This is called string interpolation:
let name = "Andy"
print("Hello, \(name)!")
Hello, Andy!
Special characters
A backslash starts an escape sequence. Use it for a quote inside a string, a new line or a literal backslash:
print("She said \"hi\"")
print("Line one\nLine two")
print("C:\\temp")
She said "hi"
Line one
Line two
C:\temp
Empty strings and length
A string may be empty: "". Printing it outputs just a blank line. The count property gives the number of characters, so "hello".count is 5.
Joining and multi-line strings
The + operator joins two strings into a new one. For text that spans several lines, Swift offers triple quotes. The opening and closing """ each sit on their own line, and the lines between them become the text:
print("Hello, " + "world")
let poem = """
Roses are red
Violets are blue
"""
print(poem)
Hello, world
Roses are red
Violets are blue
Storing a string
A string can be kept in a constant or a variable so you can reuse it. Swift works out the type from the value, and you may also write it out:
let greeting: String = "Hi there"
print(greeting)
Hi there
Common mistakes
- Forgetting a quote. This code is missing the closing quote:
Swift cannot tell where the string ends, so the compiler reports anprint("Welcome!)unterminated string literalerror and the program does not run. Swift reports these problems when it compiles the program, before anything is printed. - Single quotes. Swift strings use double quotes. Text in single quotes, such as
'Hello', is rejected by the compiler with an error that says to use". - Mixing quote styles. A string opened with
"must also be closed with". - Quotes around a variable.
print("name")prints the word name, not the value of a variable calledname.
The exercise
The task is to show the text I love coding! in the console. Pass that text, in double quotes, to print:
print("I love coding!")
The output is the text without its quotes. The exclamation mark and the capital letters belong to the string, so copy them exactly.
Key takeaways
- A string is text written inside double quotes.
- The quotes mark the start and end of the text and are not printed.
"12"is text and12is a number.\(value)inserts a value, and\"and\nare escape sequences.- A missing quote is a compile error, so the program does not run at all.
