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
- • Create and join strings with + and +=
- • Splice values into text with interpolation \\(value)
- • Write multiline literals with triple quotes """
- • Use common methods: count , isEmpty , uppercased() , contains
- • Loop over a string's Character values
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.
Pro Tips
- 💡 Prefer interpolation over + for readability — especially when mixing numbers and text.
- 💡 String methods don't mutate. uppercased() returns a new string; the original is untouched.
- 💡 Trim whitespace with trimmingCharacters(in: .whitespaces) before validating user input.
- 💡 Need positional access? Convert with Array(myString) to index by integer when necessary.
Common Errors (and the fix)
- Single quotes don't work — 'a' is invalid. Swift text always uses "double quotes" .
- "Cannot subscript a value of type 'String' with an index of type 'Int'" — strings aren't integer-indexed. Use first / last or convert to an Array .
- Forgetting the backslash — "Hi (name)" prints literally. Interpolation needs \\(name) .
- "Binary operator '+' cannot be applied to 'String' and 'Int'" — convert the number first: "Age: " + String(age) , or just use interpolation.
📋 Quick Reference
Task
Code
Result
Interpolate
"Hi \\(name)"
Hi Sam
Length
s.count
number of chars
Empty?
s.isEmpty
true / false
Uppercase
s.uppercased()
new string
Contains
s.contains("x")
Multiline
"""...lines..."""
spans lines
Frequently Asked Questions
Mini-Challenge: Greeting Card
Build it, run it, and check your output against the example in the comments.
🎉 Lesson Complete!
- ✅ Strings use "double quotes" ; join with + / +=
- ✅ Interpolation with \\(value) beats concatenation for readability
- ✅ Multiline literals use triple quotes """
- ✅ Methods like count , isEmpty , uppercased() , contains return new values
- ✅ Next lesson: Arrays
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.
Continue this course
- Previous: Optionals & nil Safety
- Next: Arrays