Strings & String Interpolation

Text is everywhere in real apps. By the end of this lesson you'll create strings, splice values into them with interpolation \\(...) , write multiline literals with """ , and reach for the most common String methods with confidence.

Learn Strings & String Interpolation in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Creating & Joining Strings

A String is text in double quotes. Join strings with + , grow a var with += , and inspect length with count or emptiness with isEmpty .

2️⃣ String Interpolation

Interpolation drops a value straight into text with \\(...) — a backslash, then your expression in parentheses. You can run a real calculation inside the parentheses, not just name a variable.

3️⃣ Multiline Strings

Wrap text in triple quotes """ (on their own lines) to span multiple lines without escapes. Interpolation works inside them too — perfect for templates and messages.

4️⃣ Common Methods & Characters

Swift's String API is rich. Methods like uppercased() , contains , and hasPrefix return new values without changing the original. Looping over a string yields each Character — Swift's single-character type.

Your turn. Fill in the blanks, then run it and check the output.

📋 Quick Reference

Build it, run it, and check your output against the example in the comments.

Practice quiz

How do you write a string literal in Swift?

  • With 'single quotes'
  • With backticks
  • With "double quotes"
  • With /slashes/

Answer: With "double quotes". Swift string literals always use double quotes.

What is the correct string interpolation syntax?

  • \(name)
  • ${name}
  • #{name}
  • {name}

Answer: \(name). Swift interpolates with a backslash and parentheses: \(name).

Which delimiter begins and ends a multiline string literal?

  • Single "
  • Triple '''
  • <<<
  • Triple """

Answer: Triple """. Multiline strings are wrapped in triple double-quotes """.

What does "abc".count return?

  • 2
  • 3
  • 4
  • nil

Answer: 3. count returns the number of characters; "abc" has 3.

What is the type of a single character extracted from a String?

  • Character
  • String
  • Char
  • UInt8

Answer: Character. Swift's single-character type is Character.

How do you check whether a String is empty?

  • s.empty
  • s.count == nil
  • s.isEmpty
  • s == null

Answer: s.isEmpty. The isEmpty property is true when the string has no characters.

What does "Hello".uppercased() return?

  • "hello"
  • "HELLO"
  • "Hello"
  • It mutates in place and returns nothing

Answer: "HELLO". uppercased() returns a new uppercase string.

Can you concatenate two strings with the + operator?

  • No, you must use append only
  • Only with numbers
  • Only inside interpolation
  • Yes, "a" + "b" gives "ab"

Answer: Yes, "a" + "b" gives "ab". The + operator joins two strings into a new string.

Which method tells you if a string contains a substring?

  • s.has("x")
  • s.contains("x")
  • s.includes("x")
  • s.find("x")

Answer: s.contains("x"). contains(_:) returns a Bool indicating presence of the substring.

Are Swift Strings value types (copied on assignment)?

  • No, they are reference types
  • Only multiline strings
  • Yes, String is a value type
  • Only empty strings

Answer: Yes, String is a value type. String is a value type, so assigning it makes an independent copy.