Kotlin Output

Kotlin gives you two simple functions for writing to the console, println and print, plus string templates for inserting values into text.

Printing to the Console

The println() function is the most common way to produce output in Kotlin. It writes its argument to the standard output stream and then moves to a new line, so the next thing you print starts fresh below it.

Printing Multiple Lines

fun main() {
    println("Line one")
    println("Line two")
    println("Line three")
}

println vs print

print() behaves just like println() except that it does not add a newline afterward. That means anything printed next will continue on the same line, which is useful when you want to build up a single line of output from several pieces.

FunctionAdds a Newline?
println(...)Yes, moves to the next line automatically
print(...)No, subsequent output continues on the same line

Combining print and println

fun main() {
    print("Name: ")
    println("Ada Lovelace")
    print("Role: ")
    print("Programmer")
}

Printing Variables and Expressions

Rather than concatenating strings with the + operator, Kotlin lets you embed values directly inside a string using string templates. A simple $variableName inserts a variable's value, while ${expression} lets you insert the result of any expression, including function calls and property access.

String Templates in Action

fun main() {
    val product = "Keyboard"
    val price = 49.99
    val quantity = 2
    println("$quantity x $product = ${price * quantity}")
}
  • Use println when each piece of output should start on its own line
  • Use print when you're assembling one line from multiple statements
  • Prefer string templates ($var or ${expr}) over manual string concatenation
  • Escape a literal dollar sign with \$ when you need one in your output
Note: Anything more complex than a single variable name - like price * quantity above - needs to be wrapped in curly braces: ${price * quantity}.

Exercise: Kotlin Output

What is the key difference between println() and print()?