๐ŸŽ“ A1Learner
๐Ÿ”

Kotlin Output

Output is what a program shows on the screen. The commands println and print put values on the screen as output.

Two prints on one line

Both print commands write on the same line. No new line sits between the two pieces.

fun main() {
    print("Hello, ")
    print("World")
}

print then println

println ends the line that print started. The next value starts below that line.

fun main() {
    print("Hello, ")
    println("World")
    println("Next line")
}

An empty line between two lines

println with nothing between its brackets shows an empty line. A blank line sits between First and Second.

fun main() {
    println("First")
    println()
    println("Second")
}

Example

fun main() {
    print("Loading")
    println(" done")
    println()
    println("Ready")
}

Key points

  • println: a command that shows a value and then moves to a new line.
  • print: a command that shows a value and stays on the same line.
  • Use println() to show an empty line.
  • One program can mix print and println.

In this example, print shows Loading and stays on the same line. println adds a space and the word done on that line and then moves to a new line. The empty println shows a blank line. The last println shows Ready.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Kotlin Playground

Exercise

Which command shows a value and then moves to a new line?