Print

As you start your journey into Swift programming, one of the first things you will learn is how to print information. Printing is a simple yet essential tool in programming. It lets you show messages or information on the screen, which is especially helpful for understanding what your code is doing.

In Swift, printing is done using the print() function. This function allows you to display text, numbers, and even results from operations directly to the console.

Let’s take a look at how to use the print() function with some examples.

1. Printing Text (Strings)

In Swift, a string is a sequence of characters, like words or sentences. The print() function can display strings to the console. To create a string, you wrap your text in double quotes.

Here’s an example of printing a simple string:

print("Hello, Swift!")

When you run this, you will see the following output in the console:

Hello, Swift!

2. Printing Numbers

You can also print numbers. In Swift, numbers like 10 or 3.14 are considered numeric values. Here’s an example of printing a whole number:

print(10)

Output:

10

Or, you can print a decimal number:

print(3.14)

Output:

3.14

3. Printing the Result of Adding Two Numbers

Let’s start by printing the result of adding two numbers together.

print(5 + 7)

Output:

12

In this example, Swift will calculate 5 + 7 and print the result, which is 12.

4. Printing Multiple Items

You can also print multiple pieces of information at once. If you want to display more than one value on the same line, just separate them with commas in the print() function. Swift will automatically add a space between the printed values.

print("The result is", 5 + 7)

Output:

The result is 12

In this case, it prints both the text “The result is” and the result of 5 + 7.

You can mix different types of data too! For example, if you want to print a sentence with a number and some words, you can do it like this:

print("The result is", 3, "apples.")

Output:

The result is 3 apples.

Here, “The result is” is a string, 3 is a number, and “apples.” is another string. print() will automatically add spaces between them.

You can even use multiple numbers, like this:

print("There are", 3, "apples and", 5, "oranges.")

Output:

There are 3 apples and 5 oranges.

This approach is very useful when you want to print a sentence with mixed types of information, such as strings and numbers, in a single line. Swift handles all the spacing for you.

5. Printing with Line Breaks

By default, the print() function adds a line break after each print statement. This means each print() command starts on a new line in the console.

Here’s an example with two print statements:

print("This is the first line.")

print("This is the second line.")

Output:

This is the first line.

This is the second line.

6. Printing with Special Characters

Swift also lets you include special characters in strings, like newlines or tabs. For example, if you want to start a new line within the printed message, you can use \n (newline character):

print("Hello,\nSwift!")

Output:

Hello,

Swift!

In this case, \n causes the text “Swift!” to be printed on a new line.