Build expressive strings
Work with text safely and format readable output.
- 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.
1let firstName = "Alex"
2let points = 42
3let message = "Nice work, \(firstName)! Score: \(points)"
4print(message)
5print("Characters: \(message.count)")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.
Greet by name
Read a name and print Hello, <name>! using interpolation.
- Name input
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…