Menu
Coddy logo textTech

Println Function

Part of the Fundamentals section of Coddy's Kotlin journey. Lesson 35 of 93.

You've been using println() throughout this course to display output. Now let's take a closer look at how it works and explore its sibling function, print().

The println() function prints text to the console and automatically moves the cursor to a new line afterward. println() adds its newline after the argument, so it continues on the current line when a previous print() has not ended that line:

fun main() {
    println("First line")
    println("Second line")
}
// Output:
// First line
// Second line

In contrast, print() outputs text without adding a newline. Subsequent output continues on the same line:

fun main() {
    print("Hello ")
    print("World")
    println("!")
    println("New line here")
}
// Output:
// Hello World!
// New line here

Notice how "Hello ", "World", and "!" all appear on the same line because the first two use print(). The println("!") adds the exclamation mark and then moves to a new line for the final output.

Both functions can print any type of value: strings, numbers, booleans, or even expressions:

fun main() {
    println(42)
    println(3 + 5)
    println(true)
}
challenge icon

Challenge

Easy

The starter reads a passenger name. Use print for "Passenger: ", then println for the name so they appear together on one line. On the next line print Boarding soon. Do not print an input prompt.

Try it yourself

fun main() {
    val passenger = readLine()!! // Provided input
    // Write the two output lines.
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Kotlin playground