Start learning free

LESSON 2 OF 415 · WELCOME TO SWIFT

Introduction to Strings in Swift

Lesson Read the full explanation

TL ; DR:

  • To create a string in Swift, we put some text inside double quotes ("Hello world").


  • Be careful not to forget any quote! This code missing a " at the end:

    print("Welcome!)

    would make the compiler report error: unterminated string literal



Full lesson:

How boring would our life as humans be if we communicated only using numbers? Luckily we have letters, words and languages to express what we think!

We can also use letters and words in Swift to express more meaningful messages like we do in real life.


Strings:

In programming, a string is any block of text e.g. any sequence of characters from your keyboard (letters, numbers, spaces, symbols, etc.).

To create a string in Swift, we put some text inside double quotes like this "Hello world".

For example, we can use strings inside the print() function for printing messages to the console:

print("My name is Andy")
print("Welcome to AlgoCademy!")

The output of this code is:

My name is Andy
Welcome to AlgoCademy!

As you can see, the quotes are not printed. That's because quotes are not part of the string. Their job is solely to let Swift know when a string declaration starts and when it ends.


Assignment
Follow the Coding Tutorial and let's work with strings!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    SWIFT · WELCOME TO SWIFT

    Introduction To Strings in Swift, explained

    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:
      print("Welcome!)
      
      Swift cannot tell where the string ends, so the compiler reports an unterminated string literal error 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 called name.

    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 and 12 is a number.
    • \(value) inserts a value, and \" and \n are escape sequences.
    • A missing quote is a compile error, so the program does not run at all.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free