The print function shows messages or values on the screen. It helps you see what your program is doing.
Print function for output in Swift
print(<message>)The message can be text in quotes or a variable.
Each print shows the message on a new line.
print("Hello, world!")
let name = "Anna" print(name)
print("Sum is", 5 + 3)
This program prints a sentence with a number stored in a variable.
let age = 10 print("I am", age, "years old.")
You can print multiple items separated by commas; Swift adds spaces automatically.
Use \" to include quotes inside text if needed.
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.
Practice
What does the print function do in Swift?
Solution
Step 1: Understand the purpose of print
Theprintfunction is used to display output on the screen, such as text or variable values.Step 2: Compare options with print's function
Only It shows text or values on the screen. describes showing text or values on the screen, which matchesprint's purpose.Final Answer:
It shows text or values on the screen. -> Option AQuick Check:
print function = display output [OK]
- Thinking print saves data
- Confusing print with variable creation
- Assuming print deletes data
Which of the following is the correct way to print the text Hello, Swift! in Swift?
?
Solution
Step 1: Identify correct Swift print syntax
Swift usesprint()with double quotes for strings.Step 2: Check each option
print("Hello, Swift!") usesprint("Hello, Swift!"), which is correct. Others use wrong functions or missing parentheses/quotes.Final Answer:
print("Hello, Swift!") -> Option CQuick Check:
Correct print syntax = print("Hello, Swift!") [OK]
- Using single quotes for strings
- Missing parentheses
- Using wrong function names
What will be the output of this Swift code?
let name = "Anna"
print("Hello, \(name)!")Solution
Step 1: Understand string interpolation
The syntax\(name)inserts the value of variablenameinto the string.Step 2: Substitute variable value
Sincenameis "Anna", the printed string becomes "Hello, Anna!".Final Answer:
Hello, Anna! -> Option DQuick Check:
String interpolation = Hello, Anna! [OK]
- Printing variable name literally
- Forgetting \( ) syntax
- Syntax errors in string interpolation
Find the error in this Swift code snippet:
let age = 25
print("Age is: " + age)Solution
Step 1: Analyze the print statement
The code tries to add a String and an Int using +, which is not allowed in Swift.Step 2: Identify correct way to combine String and Int
Use string interpolation like"Age is: \(age)"instead of + operator.Final Answer:
Cannot add String and Int directly. -> Option BQuick Check:
String + Int needs interpolation [OK]
- Trying to add String and Int
- Missing string interpolation
- Assuming + works for different types
You want to print a list of numbers from 1 to 3 on the same line separated by spaces using Swift. Which code will do this correctly?
?
Solution
Step 1: Understand print's separator parameter
Swift'sprintcan take multiple values and aseparatorargument to join them with a string.Step 2: Check each option
print(1, 2, 3, separator: " ") usesseparator: " "correctly to print "1 2 3". Adding numbers and strings causes type error, print("1, 2, 3") prints commas instead of spaces, wrong parameter name fails.Final Answer:
print(1, 2, 3, separator: " ") -> Option AQuick Check:
Use separator: " " to join values [OK]
- Using wrong parameter name sep
- Trying to add Int and String
- Printing fixed string instead of variables
Ready to prove it?
Pass the test and this pattern moves unknown
