Swift Tutorials: Print Statement (print()) šŸŽÆ

beginner
6 min

Swift Tutorials: Print Statement (print()) šŸŽÆ

Welcome to our Swift Tutorials! In this lesson, we'll dive deep into the Print Statement, a fundamental concept in Swift programming. By the end of this tutorial, you'll be able to display text, numbers, and variables using the print() function. Let's get started! šŸ“

Understanding the Print Statement (print())

The print() function is Swift's built-in function used to output text, numbers, and variables to the console. It's an essential tool for seeing the results of your code and debugging your Swift programs. šŸ’”

Syntax and Usage

To use the print() function, simply type print() followed by what you want to print, surrounded by double quotes for strings or without quotes for numbers and variables.

swift
print("Hello, World!") // Output: Hello, World! print(42) // Output: 42

šŸ“ Note: The print() function can also accept multiple arguments separated by commas, which will be printed on separate lines.

swift
print("Apple", "Banana", "Cherry") // Output: // Apple // Banana // Cherry

Adding a Newline

By default, print() prints output on the same line. However, you can force a newline by adding a backslash followed by a newline character (\n) at the end of your string.

swift
print("Hello,\nWorld!") // Output: // Hello, // World!

Quiz

Quick Quiz
Question 1 of 1

Which Swift function is used to display text, numbers, and variables in the console?

Now that you've grasped the basics of the print() function, let's dive deeper into working with variables and formatting output. šŸ’”


Working with Variables

Variables are an essential part of Swift programming. In the next section, you'll learn how to print the values of variables using the print() function.

Continue to the next section: Working with Variables


Formatting Output

Formatting output is crucial for creating clean and easy-to-read console output. In this section, we'll discuss how to format output using Swift's formatting placeholders.

Continue to the next section: Formatting Output


Working with Variables

Now that you know how to use the print() function, let's explore working with variables in Swift.

Declaring Variables

In Swift, you can declare variables using the var keyword, followed by a name and an initial value.

swift
var myName = "John Doe" print(myName) // Output: John Doe

Changing Variable Values

You can change the value of a variable at any time during your program's execution.

swift
myName = "Jane Doe" print(myName) // Output: Jane Doe

Constants

If you want to declare a variable that cannot be changed, you can use the let keyword instead of var.

swift
let pi = 3.14159 print(pi) // Output: 3.14159

šŸ“ Note: Constants are immutable, meaning you can't change their values after they're assigned.

Quiz

Quick Quiz
Question 1 of 1

Which keyword is used to declare a variable that cannot be changed in Swift?

Formatting Output

Formatting output is essential for creating clear and easy-to-read console output. Swift provides several formatting options to help you achieve this.

Using Placeholders

Swift uses placeholders to format output. You can use \(variableName) to include the value of a variable in your output.

swift
let myName = "John Doe" print("Hello, \((myName)!") // Output: Hello, John Doe!

Adding Newlines

To add a newline between output, you can use \n just like in the basic usage of the print() function.

swift
print("Apple") print("Banana\nCherry") // Output: // Apple // Banana // Cherry

Formatting Numbers

You can format numbers using placeholders like \(integer.description), \(double.description), and \(double.precision(significantDigits:)).

swift
let pi = 3.14159 let formattedPi = String(format: "%.2f", pi) // Uses printf-style formatting print("Pi: \(pi)") // Output: Pi: 3.14159 print("Formatted Pi: \(formattedPi)") // Output: Formatted Pi: 3.14 print("Formatted Pi with 4 significant digits: \(String(format: "%.4f", pi))") // Output: Formatted Pi with 4 significant digits: 3.1416

Quiz

Quick Quiz
Question 1 of 1

How do you include the value of a variable in your output using placeholders in Swift?

And there you have it! You now know how to use the print() function to output text, numbers, and variables in Swift. With the knowledge of variables and formatting output, you can create clean, practical, and professional-looking console output for your projects.

Happy coding! šŸ’”