Loading
0x20Lesson 3 of 7

Build expressive strings

Work with text safely and format readable output.

15 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Use interpolation and basic string operations to build messages.

Swift strings are Unicode-correct and safe by default. Use interpolation (\(...)) to compose output naturally. You can transform strings with methods like uppercased() and count characters with .count.

main.swift
1let firstName = "Alex"
2let points = 42
3let message = "Nice work, \(firstName)! Score: \(points)"
4print(message)
5print("Characters: \(message.count)")
Output
Nice work, Alex! Score: 42
Characters: 27

Interpolation keeps formatting readable and avoids fragile manual concatenation. Swift strings store extended grapheme clusters, so character counting is human-friendly.

Key takeaways

  • Use interpolation and basic string operations to build messages.

  • Swift favors clear intent and safe defaults.

  • Compiler feedback is part of the learning loop.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Swift

These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.

Exercise 1

Greet by name

+25 XP

Read a name and print Hello, <name>! using interpolation.

  • Name input
main.swift
Loading editor…

Swift lessons are authored for Swift 6.4; exercises run on Swift 6.0.1 via Wandbox, a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: