0
0
Swiftprogramming~5 mins

Print function for output in Swift

Choose your learning style9 modes available
Introduction

The print function shows messages or values on the screen. It helps you see what your program is doing.

You want to show a greeting message to the user.
You need to check the value of a variable while testing your code.
You want to display results or answers from calculations.
You want to show instructions or information during a program run.
Syntax
Swift
print(<message>)

The message can be text in quotes or a variable.

Each print shows the message on a new line.

Examples
Prints the text Hello, world! on the screen.
Swift
print("Hello, world!")
Prints the value stored in the variable name.
Swift
let name = "Anna"
print(name)
Prints text and the result of 5 + 3 together.
Swift
print("Sum is", 5 + 3)
Sample Program

This program prints a sentence with a number stored in a variable.

Swift
let age = 10
print("I am", age, "years old.")
OutputSuccess
Important Notes

You can print multiple items separated by commas; Swift adds spaces automatically.

Use \" to include quotes inside text if needed.

Summary

The print function shows messages or values on the screen.

Use it to check your program's behavior or show results.

It can print text, variables, or both together.