Kotlin println() – Print with a New Line

In Kotlin, use println() to print a value to the standard output and then move the cursor to a new line. Each following println() call therefore starts its output on the next line.

You can use println() with strings, numbers, characters, Boolean values, variables, and expressions. Calling println() without an argument prints only a line break.

Kotlin println() syntax

The basic syntax for printing a value followed by a new line is:

</>
Copy
println(value)

To print only a new line, call println() without passing a value:

</>
Copy
println()

Example 1 – Kotlin println() – String message

In this example, we shall print a string to the standard output console.

Kotlin Program – example.kt

</>
Copy
/**
 * Kotlin - println() - Print with New Line
 */
fun main(args: Array<String>) {
    var str = "Hello World"
    println(str)
    println(str)
}

Run the above Kotlin program, and you shall see the following output print to standard console output.

Output

Hello World
Hello World

The first println(str) prints Hello World and then adds a line break. The second call therefore prints the same string on the next line. It also adds a new line after its own output.

Example 2 – Kotlin println() – Other Datatypes

In this example, we shall print messages of different datatypes to the standard output console.

Kotlin Program – example.kt

</>
Copy
/**
 * Kotlin - println() - Print with New Line
 */
fun main(args: Array<String>) {
    println("Hello World") //string
    println(35) //int
    println(12345L) //long
    println(0b00001011) //byte
    println(2.35) //double
    println(41.253F) //float
    println('m') //Char
    println(true) //Boolean
    println() //no argument
}

Run the above Kotlin program, and you shall see the following output print to standard output.

Output

Hello World
35
12345
11
2.35
41.253
m
true

println() displays the textual representation of each value. Numeric literal suffixes such as L and F are part of the source code and are not included in the printed result. Also note that the binary literal 0b00001011 has the decimal value 11; an integer literal such as this is an Int by default unless it is explicitly converted or used in an appropriate typed context.

The final println() has no argument, so it prints an empty line. That blank line comes after true and may not be visually obvious at the end of an output block.

Kotlin print() vs println(): same line and new line output

Kotlin provides both print() and println(). The difference is what happens after the value is printed: print() leaves the cursor on the same line, while println() adds a line break.

FunctionAfter printing
print(value)Continues on the same line
println(value)Moves to the next line
println()Prints a new line only

For example, the following program mixes print() and println():

</>
Copy
fun main() {
    print("Hello ")
    print("Kotlin")
    println()
    println("Next line")
}

Output

Hello Kotlin
Next line

The two print() calls write text on the same line. The empty println() then moves output to the next line before the final message is printed.

Print Kotlin variables with println()

You can pass a variable directly to println(). Kotlin evaluates the variable and prints its current value.

</>
Copy
fun main() {
    val language = "Kotlin"
    val version = 2

    println(language)
    println(version)
}

Output

Kotlin
2

Print variables and expressions using Kotlin string templates

When text and variable values need to appear together, Kotlin string templates can be passed directly to println(). Use $variable for a variable or ${expression} for an expression.

</>
Copy
fun main() {
    val name = "Sam"
    val score = 42

    println("Name: $name")
    println("Next score: ${score + 1}")
}

Output

Name: Sam
Next score: 43

Add a new line inside Kotlin output with \n

println() automatically adds a new line after its argument. If you need a line break inside a string, use the newline escape sequence \n.

</>
Copy
fun main() {
    println("First line\nSecond line")
}

Output

First line
Second line

Here, \n creates the line break between the two pieces of text, and println() adds another newline after Second line.

Print several Kotlin values on one line

If values should remain on the same output line, use print() and include any required separators such as spaces yourself.

</>
Copy
fun main() {
    print("One ")
    print("Two ")
    print("Three")
}

Output

One Two Three

Use println() instead when each value should finish with a line break.

Kotlin println() key points

  • println(value) prints a value and then a newline.
  • print(value) prints without automatically adding a newline.
  • println() with no argument prints an empty line.
  • \n can create a newline within a string.
  • Variables and expressions can be printed directly, including through Kotlin string templates.

In this Kotlin Tutorial, we learned how println() prints values with a trailing new line, how it differs from print(), and how to create additional line breaks in Kotlin output.