0Pricing
Swift Academy · Lesson

String Fundamentals and Interpolation

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

String Fundamentals and Interpolation is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “String Fundamentals and Interpolation” lesson free?

Yes — the full text of “String Fundamentals and Interpolation” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

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