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.

Swift

What you see:

text

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.
Swift

2. Printing multiple things in one line

You can pass many values to print() — it puts a space between them by default.

Swift

Output:

text

You can control the separator between items:

Swift

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.

Swift

Output (all on one line):

text

Very common pattern — printing a progress bar or numbers in one line:

Swift

Output:

text

4. The most important way for real messages: String Interpolation

Almost every real print() uses \(…) — this is called string interpolation.

Swift

You can put expressions inside too:

Swift

Output:

text

5. Printing nicely formatted numbers (very important!)

Numbers often look ugly by default:

Swift

Better ways:

Way 1: Simple fixed decimals with String(format:)

Swift

Way 2: Add thousands separator

Swift

Way 3: Modern NumberFormatter (clean & flexible)

Swift

Output:

text

6. Printing special characters (emoji, new lines, tabs)

Swift

Output:

text

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

Swift

Example 2: Progress style

Swift

Example 3: List with numbers

Swift

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 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *