Swift Input Output with 7 Examples

In this article, We are going to cover Swift Input Output with Examples.

This article gives syntax of part of codes which are requiring printing the statements in multiple ways. As well as after compiling the code, how to take input from user at the time of running in swift Language.

Swift Input Output with Examples

#1: Print by using  direct print function

print (“HI, HOW ARE YOU”)



In above syntax, we are using direct print function to print the given message instead of using any variable

#2: Print by using variable

var Msg= "Hello"
print (Msg)



In above syntax, Variable named as “Msg”   is declared in first line. That variable is assigned to message which we want to print.

Second line print (variable name) that is print (Msg) will automatically print our message which is assigned to variable “Msg”

#3: Print constant




Above statement is used to directly print the constant value which is given in round brackets. Here there is no need to use quotes to print constant.

#4: Print two statements without breaking of line

print("My name is Geeta!!", terminator: "")
print("I love Swift.")


My name is Geeta!! I love Swift.

In the above statement, terminator is used to print two statements. My name is Geeta and I love swift on the same line without breaking the line.

#5: Print multiple items on same line

print(“ Geeta”, 90, “ Hello”,  separator, “.”)


Geeta. 90. Hello

Above print statement contains three items named Geeta, 90, Hello which we want to print on the same line separated with full stop. Therefore, a parameter separator is used . Value of the separator is given in double quotes “.”.

In output we can see all items get printed with the separator of full stop. Instead of full stop we can use any other operator as well to separate items .

#6: Printing items on multiple lines

print(“banana , \r apple,\r pineapple, \r chiku”)



Here /r is used to print a particular item on a new line. This syntax is used whenever we want to list items in such a way that every item will be on new line.

#7: Taking input from user

print("Please enter your favorite game")
let name = readLine()
print("Your favorite game is \(name!).")


Please enter your favorite game
Your favorite game is Cricket

In the above statement the “readLine” function is used for taking input from the user at run time.

Here variable “name” is used for taking input from the user.\(name!) will print the name of the favorite game entered by the user.

In this way we can give any name to declare a variable.

Extension of your code/ program

As we have seen in first article on swift. Before writing our code, we are selecting swift language in Xcode.

After writing code, automatically our code/ program will save with extension .swift

We have covered Swift Input Output with Examples.


We have covered Swift Input Output with Examples.

Related Articles:

Swift Variables Strings Array Loop and Functions with Example

iOS Architecture, Concepts and Xcode Installation [4 Steps]

Arrays in swift with examples


iOS official page

Geeta Atkar

I am Geeta Atkar PhD(Pursuing), ME (Computer Engg), BE (Information Technology) , Assistant Professor, G H Raisoni College of Engg and Management, Pune and Research Scholar, Vellore Institute of Technology, Chennai

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap