Chapter 6: Print Text
print text in Swift (showing text on the screen / in the console).
I will explain it slowly, step by step, like we’re sitting together and I’m showing you on my laptop — with many small, runnable examples, common mistakes, useful tricks, and real-life situations where each method is used.
Let’s begin!
1. The most basic and most used way: print()
This is the number one function you will use when learning Swift, writing playgrounds, command-line tools, server-side code, or just debugging.
|
0 1 2 3 4 5 6 7 8 |
print("Hello!") print("Namaste") print("I am learning Swift") |
What you see:
|
0 1 2 3 4 5 6 7 8 |
Hello! Namaste I am learning Swift |
Important facts about print():
- It automatically adds a new line at the end (\n)
- You can print almost anything: String, Int, Double, Bool, arrays, etc.
|
0 1 2 3 4 5 6 7 8 9 |
print(42) // number print(3.14159) // decimal print(true) // boolean print(["apple", "banana"]) // array |
2. Printing multiple things in one line
You can pass many values to print() — it puts a space between them by default.
|
0 1 2 3 4 5 6 7 8 9 10 |
let name = "Aarav" let age = 19 let city = "Hyderabad" print("Name:", name, "Age:", age, "City:", city) |
Output:
|
0 1 2 3 4 5 6 |
Name: Aarav Age: 19 City: Hyderabad |
You can control the separator between items:
|
0 1 2 3 4 5 6 7 8 9 10 |
print("Jan", "Feb", "Mar", "Apr", separator: " → ") // Jan → Feb → Mar → Apr print(1, 2, 3, 4, 5, separator: "-") // 1-2-3-4-5 |
3. Controlling the end of the line (no newline, custom ending)
By default print() adds \n (new line). You can change this with the terminator parameter.
|
0 1 2 3 4 5 6 7 8 |
print("Loading", terminator: "") print("...", terminator: "") print(" Done!") |
Output (all on one line):
|
0 1 2 3 4 5 6 |
Loading... Done! |
Very common pattern — printing a progress bar or numbers in one line:
|
0 1 2 3 4 5 6 7 8 9 10 |
for i in 1...10 { print(i, terminator: " ") } print() // just to add final newline |
Output:
|
0 1 2 3 4 5 6 |
1 2 3 4 5 6 7 8 9 10 |
4. The most important way for real messages: String Interpolation
Almost every real print() uses \(…) — this is called string interpolation.
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
let username = "priya_dev" let score = 87 let level = 12 print("Welcome back, \(username)!") print("Your current score is \(score) points.") print("You are at level \(level). Great job!") |
You can put expressions inside too:
|
0 1 2 3 4 5 6 7 8 9 10 |
let price = 450 let quantity = 3 print("Total amount: ₹\(price * quantity)") print("Next year you will be \(age + 1) years old") |
Output:
|
0 1 2 3 4 5 6 7 |
Total amount: ₹1350 Next year you will be 20 years old |
5. Printing nicely formatted numbers (very important!)
Numbers often look ugly by default:
|
0 1 2 3 4 5 6 7 |
let bigNumber = 1234567.894 print(bigNumber) // 1234567.894 |
Better ways:
Way 1: Simple fixed decimals with String(format:)
|
0 1 2 3 4 5 6 7 8 9 10 |
let pi = 3.1415926535 let money = 9876543.21 print(String(format: "Pi = %.2f", pi)) // Pi = 3.14 print(String(format: "Amount = %.2f", money)) // Amount = 9876543.21 |
Way 2: Add thousands separator
|
0 1 2 3 4 5 6 7 8 9 |
print(String(format: "Score: %d", 1234567)) // Score: 1234567 print(String(format: "Score: %,d", 1234567)) // Score: 1,234,567 print(String(format: "Price: ₹%,.2f", 9876543.21)) // Price: ₹9,876,543.21 |
Way 3: Modern NumberFormatter (clean & flexible)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
let formatter = NumberFormatter() formatter.numberStyle = .decimal formatter.groupingSeparator = "," formatter.minimumFractionDigits = 2 formatter.maximumFractionDigits = 2 if let formatted = formatter.string(from: 1234567.89 as NSNumber) { print("Total: ₹\(formatted)") } |
Output:
|
0 1 2 3 4 5 6 |
Total: ₹1,234,567.89 |
6. Printing special characters (emoji, new lines, tabs)
|
0 1 2 3 4 5 6 7 8 9 10 |
print("I ❤️ Swift") // emoji works perfectly print("Line 1\nLine 2\nLine 3") // \n = new line print("Name\tAge\tCity") // \t = tab print("Aarav\t19\tHyderabad") |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
I ❤️ Swift Line 1 Line 2 Line 3 Name Age City Aarav 19 Hyderabad |
7. Common beginner mistakes (and how to fix them)
| Mistake | Wrong code | Correct / better way |
|---|---|---|
| Forgetting quotes | print(Hello) | print(“Hello”) |
| Using + for many parts | print(“Score: ” + score) | print(“Score:”, score) or “\(score)” |
| Too many force-unwraps in print | print(optional!) | print(optional ?? “N/A”) |
| Printing without newline when needed | print(“Loading”) many times | Use terminator: ” ” or terminator: “” |
| Not formatting money/numbers | print(1234567.8) | Use %.2f or NumberFormatter |
8. Small real-world examples you can copy-paste
Example 1: Simple user info
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
let name = "Sneha" let points = 1420 let rank = 7 print("╔══════════════════════════════╗") print("║ PLAYER INFO ║") print("╠══════════════════════════════╣") print("║ Name: \(name.padding(toLength: 20, withPad: " ", startingAt: 0)) ║") print("║ Points: \(String(format: "%,d", points)) ║") print("║ Rank: #\(rank) ║") print("╚══════════════════════════════╝") |
Example 2: Progress style
|
0 1 2 3 4 5 6 7 8 9 10 11 |
print("Downloading files... ", terminator: "") for _ in 1...5 { print(".", terminator: "") // fake delay } print(" Done!") |
Example 3: List with numbers
|
0 1 2 3 4 5 6 7 8 9 10 |
let fruits = ["Mango", "Banana", "Apple", "Orange", "Guava"] for (index, fruit) in fruits.enumerated() { print("\(index + 1). \(fruit)") } |
Summary – Quick Cheat Sheet
| Goal | Best code example |
|---|---|
| Simple text | print(“Hello”) |
| Text + variables | print(“Hi \(name)”) |
| Multiple values | print(“Age:”, age, “City:”, city) |
| No newline | print(“Loading”, terminator: “”) |
| Custom separator | `print(a, b, c, separator: “ |
| Money / decimals | String(format: “₹%.2f”, amount) |
| Thousands separator | String(format: “%,d”, number) |
| Emoji & special chars | print(“I ❤️ Swift\tDone!”) |
Would you like to continue with:
- Reading input from the user (the opposite of printing)
- More advanced formatting (dates, percentages, currencies)
- Printing tables or aligned text
- Logging instead of print (for real apps)
- Printing in colors in terminal (ANSI codes)
Just tell me what you want to learn next — we’ll keep going in the same detailed, step-by-step way 😊
