Coding/Learn/swift/swift strings and characters
Swift Foundational Course
01.Introduction, Safety Philosophy & The Toolchain
02.Constants (let), Variables (var) & Type Inference
03.Strings, String Interpolation & Multiline Literals
04.Operators, Range Types & Nil-Coalescing
05.Control Flow: If, Switch Pattern Matching & Guard Statements
06.Optionals, Optional Binding & Optional Chaining
07.Arrays, Sets & Higher-Order Collection Operations
08.Dictionaries, Default Lookups & Tuples
09.Loops, Defer Execution & Labeled Statements
10.Functions, Argument Labels & In-Out Parameters
11.Closures, Trailing Closures & Shorthand Arguments
12.Enums, Raw Values & Associated Values (Tagged Unions)
13.Error Handling: Throwing Functions, Do-Catch & Try Options
14.Structures (struct), Value Semantics & Mutating Methods
15.Classes, Reference Semantics & Inheritance
16.Automatic Reference Counting (ARC), Weak & Unowned References
17.Properties: Stored, Computed, Observers (didSet) & Lazy
18.Protocols, Protocol Extensions & POP (Protocol-Oriented Programming)
19.Generics, Type Constraints & Associated Types
20.Capstone: Production Async/Await Cache & JSON API Engine
Course Certificate0%

Complete all lessons to earn

LearnStrings, String Interpolation & Multiline Literals
3 / 20 0%
Lesson 03 of 20
8 mins read

Strings, String Interpolation & Multiline Literals

PreviousNext Lesson
In Plain English

Swift Strings are fully Unicode-compliant value types. Learn string interpolation (\(expression)), multiline string literals (""" ... """), character counting (.count), string inspection, and string formatting.

The Mental Model

"String interpolation in Swift is like a picture frame with cut-out slots: you snap dynamic photo cards (\(user), \(score)) right into the pre-printed border."

Deep Dive: How It Works

String Interpolation: Enclose expressions inside \(expr) to embed dynamic values directly inside string literals.

Multiline String Literals: Triple quotes (""") preserve indentation, line breaks, and quotation marks without backslash escapes.

Grapheme Clusters: Swift strings handle complex Unicode emojis and accents accurately when counting characters (s.count).

Syntax Blueprint

let name = "Alice"
let greeting = "Hello, \(name)!"
let multiline = """
    Line 1
    Line 2
    """

Interpolate values with \() and create multi-line text blocks with triple quotation marks.

PointCore Rules to Remember

Point star
Value Semantics: Swift strings are value types: assigning or passing a string copies it (with Copy-on-Write optimization).
Point star
Unicode Correctness: Characters represent extended grapheme clusters; combining accents and flags count as single characters.
Point star
Helper Methods: Use .lowercased(), .uppercased(), .hasPrefix(), and .hasSuffix() for inspection.

Common Beginner Traps & How to Fix Them

Mistake:Trying to access string characters using integer indexing (e.g. str[0]).

Why it happens: Because UTF-8 graphemes vary in byte length, Swift requires String.Index (str[str.startIndex]) to guarantee O(1) correctness.

How to fix: Use string.prefix(n), or iterate characters with for char in str.

Pro Tip: You can execute arbitrary computations inside interpolation slots, e.g. "Total: \(price * 1.08)".

Live Interactive Example

Hit Run Code to see it live
String Interpolation and Multiline Formatting
Swift 5.10 / LLVM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Output Console
Click "Run Code" to view the rendered output.
How it works: Multiline string formatted the card and grapheme cluster count correctly measured the emoji as 1 character.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Format Dynamic Server Metric

  • PointDeclare let host = "api.zencompiler.com" and let port = 8080.
  • PointDeclare let isSecure = true.
  • PointPrint "Server connected to \(host):\(port) (SSL: \(isSecure))".
1
2
3
4
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates