Swift Print Variables

Swift Introduction

🖨️ Swift Print Variables

In Swift Print Variables means displaying variable values on the console.

Swift provides a simple, safe, and powerful way to do this using print().


 1. Printing a Single Variable

Output

25

 2. Printing Variable with Text (Best Method ✅)

Use string interpolation \( ).


Output

My name is Sanjit

✔ Safe
✔ Clean
✔ Recommended


 3. Printing Multiple Variables Together


 

Output

10 20

 4. Printing Different Data Types


 


 5. Printing Multiple Variables in One Sentence


 

Output

Product: iPhone, Price: ₹79999

 6. Custom Separator


Output

Swift-is-awesome

 7. Custom Terminator (No New Line)


Output

Hello Swift

 8. Printing Optional Variables



 9. Using debugPrint() for Variables

Used mainly for debugging, shows more detail for complex data.


❌ Common Mistake (Very Important)

Wrong ❌

var age = 25
print("Age is " + age) // Error

Correct ✅

print("Age is \(age)")

🧠 Summary Table

TaskCorrect Way
Print variableprint(varName)
Text + variable"Text \(var)"
Multiple varsprint(a, b)
Optional varvar ?? default
Debug outputdebugPrint()

You may also like...