Skip to main content

Swift Input and Output

Input and output is two standard mechanism. This is capable to input value by keyboards and result is producing on console screen. That mechanism are work on runtime of during program execution based on custom demand.

Input

print("Enter your name")
var name:String=readLine()!
print("Welcome, \(name)")

Output

Enter your name
lio
Welcome, lio

Output

print() This function are used to display constant string or variables values.

//define a variable
let name="Foo"

print("Welcome mister \(name)")

Output

Welcome mister Foo

In this print statement is similar to c, c++ and python programming language that are used to display a message on console screen. But note that, there is no need to import any extra library to input and output to console. print() is global function that can be used any place in source code. There structure of this function as follows.

print(_:separator:terminator:)

Terminator is an by default new line character. There is possible to modified that is in following way.

print("One",terminator : "  ") 
print("Two",terminator : "  ")
print("Three")

Output

One  Two  Three

In this example print statement is terminating by double white space. And separator can be used in following ways.

//label of separator is _ underscores
print("One",_:"...",terminator:"") 
print("Two",_:"...",terminator:"")
print("Three")

Output

One ...Two ...Three




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment