Swift Academy · Lesson

String Fundamentals and Interpolation

Creating strings, string interpolation with \(variable) and basic operations.

Lesson 4 of 412 steps

String Fundamentals and Interpolation is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

Welcome

Strings are at the heart of almost every app. Swift's `String` type is Unicode-correct, value-typed, and extremely powerful. Let's learn the essentials.

String Literals

String literals use double quotes: ```swift let greeting = "Hello, World!" var city = "Istanbul" ``` For multi-line strings use triple quotes: ```swift let poem = """ Roses are red, Swift is great. """ ```

String Concatenation

Combine strings with the `+` operator or `+=`: ```swift let first = "Hello" let second = " World" let full = first + second // "Hello World" var msg = "Hi" msg += " there" // "Hi there" ```

String Interpolation

Embed any expression inside a string with `\(expression)`: ```swift let name = "Alice" let score = 95 print("Player \(name) scored \(score) points") // Player Alice scored 95 points ``` Interpolation is safer and more readable than manual concatenation.

isEmpty and count

Check if a string is empty or get its character count: ```swift var text = "" print(text.isEmpty) // true text = "Swift" print(text.count) // 5 ``` Use `.isEmpty` instead of `text == ""` — it's more idiomatic Swift.

Common String Methods

```swift let str = "Hello, Swift!" str.uppercased() // "HELLO, SWIFT!" str.lowercased() // "hello, swift!" str.contains("Swift") // true str.hasPrefix("Hello") // true str.hasSuffix("!") // true str.replacingOccurrences(of: "Swift", with: "World") ```

String Mutability

Strings in Swift are *value types*. A `var` string is mutable; a `let` string is immutable: ```swift var mutable = "Hello" mutable.append(" World") // ✓ let immutable = "Hello" immutable.append(" World") // ❌ compile error ```

String Comparison

Compare strings with `==`, `<`, `>` operators (Unicode-aware): ```swift let a = "apple" let b = "banana" print(a == b) // false print(a < b) // true (lexicographic order) ``` Comparisons are locale-independent by default.

Raw Strings

Use `#"..."#` to disable escape sequences — useful for regex patterns or paths: ```swift let pattern = #"\d{3}-\d{4}"# // The backslashes are literal, not escape sequences let path = #"C:\Users\Alice"# ``` Interpolation still works with `\#(value)` inside raw strings.

Converting Between String and Other Types

```swift let num = 42 let str = String(num) // "42" let back = Int(str) // Optional(42) let invalid = Int("abc") // nil ``` Converting from String to numeric types returns an Optional because the string might not represent a valid number.

Quick Check

What syntax embeds a variable's value inside a Swift string literal?

Recap

Key takeaways: • String literals use `"..."`, multi-line uses `"""` • Interpolate with `\(expression)` — the cleanest way to build strings • `var` strings are mutable, `let` strings are immutable • Use `.isEmpty`, `.count`, `.contains()`, `.hasPrefix()`, `.hasSuffix()` • Converting String → numeric types yields an Optional Next lesson: for-in loops with ranges.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “String Fundamentals and Interpolation” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “String Fundamentals and Interpolation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “String Fundamentals and Interpolation”?

Creating strings, string interpolation with \(variable) and basic operations. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “String Fundamentals and Interpolation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. let vs var: Constants and Variables
  2. Type Inference and Explicit Types
  3. Numbers, Booleans and Characters
  4. String Fundamentals and Interpolation
← Back to Swift Academy