Swift Basic Input and Output

Swift Output

In Swift, we can simply use the print() function to print output. For example,

print("Swift is powerful")

// Output: Swift is powerful

Here, the print() function displays the string enclosed inside the double quotation.

Syntax of print()

In the above code, the print() function is taking a single parameter.

However, the actual syntax of the print function accepts 3 parameters

print(items: separator: terminator:)

Here,

  • items - values inside the double quotation
  • separator (optional) - allows us to separate multiple items inside print().
  • terminator (optional) - allows us to add add specific values like new line "\n", tab "\t"

Note: separator and terminator are optional. If we don't include them inside the print(), their default values: single space " " for separator and new line "\n" for terminator are used.


Example 1: Swift Print Statement

print("Good Morning!")
print("It's rainy today")

Output

Good Morning!
It's rainy today

In the above example, the print() statement only includes the items to be printed. Here, the value for terminator is not used. Hence, it takes the default value "\n".

So we get the output in two different lines.


Example 2: print() with terminator

// print with terminator space
print("Good Morning!", terminator: " ")

print("It's rainy today")

Output

Good Morning! It's rainy today

Notice that we have included the terminator: " " after the end of the first print() statement.

Hence, we get the output in a single line separated by space.


Example 3: print() with separator

print("New Year", 2022, "See you soon!", separator: ". ")

Output

New Year. 2022. See you soon!

In the above example, the print() statement includes multiple items separated by a comma.

Notice that we have used the optional parameter separator: ". " inside the print() statement. Hence, the output includes items separated by . not comma.


Example: Print Variables and Literals

We can also use the print() function to print Swift variables. For example,

var number: Double = -10.6

var name: String = "Programiz"

// print literals     
print(5)

// print variables
print(number)
print(name)

Output

5
-10.6
Programiz

Example: Print Concatenated Strings

We can also join two strings together inside a print() statement. For example,

print("Programiz is " + "awesome.")

Output

Programiz is awesome.

Here,

  • the + operator joins two strings "Programiz is " and "awesome."
  • the print() function prints the joined string

To learn more about joining strings, visit Swift Join String.


Print Variables and Strings together

In Swift, we can print a string and variable together by using string interpolation. Here, we use the backslash and bracket to print variables inside a string. For example,

var year = 2014
print("Swift was introduced in \(year)")

Output

Swift was introduced in 2014

In the above example, the string inside the print() statement includes

  • Text: Swift was introduced in
  • Variable: /(year)

Now, the print() statement takes the value of the variable year and joins it with the string.

Hence, we get the output: "Swift was introduced in 2014".


Swift Basic Input

In Swift, we cannot directly take input from the Xcode playground.

However, we can create a Command line Tool in Xcode and use the readLine() function to take input from users.

For example,

print("Enter your favorite programming language:")
let name = readLine()

print("Your favorite programming language is \(name!).")

Output

Enter your favorite programming language:
Swift
Your favorite programming language is Swift.

In the above example, we are asking users to input values. Notice the code,

let name = readLine()

Here, the readLine() takes input from the user and assigns it to the name variable.

The readLine() function doesn't return a regular string. Instead, it returns an optional string. Hence, we have used name! to forcefully unwrap the name.

To learn more about optional, visit Swift Optionals.

Note: The above program only runs if you have created a command line tool in Xcode. To learn how to create command line tools, visit Command Line Tool on macOS.

Did you find this article helpful?