Start learning free

LESSON 1 OF 348 · WELCOME TO KOTLIN

Printing to the Console in Kotlin: print and println

Lesson Read the full explanation

TL ; DR:

  • The console is a panel that displays important messages, like errors, for developers.


  • In Kotlin, we use the println() function to print something to the console. For example, this program:

    println(13)
    

    prints 13 to the console.



Full lesson:

The console is a panel that displays important messages, like errors, for developers.

Much of the work the computer does with our code is invisible to us by default. If we want to see things appear on our screen, we can print to our console directly.


println():

In Kotlin, we use the println() method to print something to the console. For example, this program:

println(13)

logs 13 to the console.

When you run this program, this is what you'll see in your console:

13


We refer to the data printed to the console as the output of the program.


Newline character:

You might have noticed the extra empty line after 13 in our program's output.

It's important to know that the println() function also prints an invisible newline character after the value we provide.

This newline character inserts a new line in the console. It's like pressing Enter in your text editor to move the cursor to the next line.

This means that if we use println() multiple times, the values will be printed on separate lines in the console:

println(1)
println(2)
println(3)

The ouput of this program would be:

1
2
3


Assignment
Follow the Coding Tutorial and let's print some values to the console!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · WELCOME TO KOTLIN

    Print in Kotlin, explained

    What printing is

    A program does most of its work out of sight. To see a value, a result or a message while the program runs, you print it to the console, the panel where a program's text output appears. Whatever a program prints is called its output. Printing is the first tool for checking what your code is doing, and every other lesson in this course uses it.

    println: print a value and move to the next line

    In Kotlin, the standard way to print is the function println. You put the value to print between its parentheses:

    println(13)
    

    The console shows:

    13
    

    Numbers don't need quotes. Text does, and it is covered in the next lesson. The difference matters: println(1998) prints a number, and println("1998") prints the four characters 1998 as text. On the screen they look the same, but they are different values to the program. For example, println(12 + 1) prints 13, while println("12" + 1) prints 121, because adding to text joins the pieces together instead of doing arithmetic.

    The newline character

    The ln at the end of println stands for line. After the value, println prints an invisible newline character, which does what pressing Enter does in a text editor: the next output starts on a new line. The newline belongs to the end of the printed line. It doesn't add an empty line after it.

    That's why several println calls put their values on separate lines:

    println(1)
    println(2)
    println(3)
    

    The output is:

    1
    2
    3
    

    print: no newline

    Kotlin also has print, which prints the value and stops there. Whatever is printed next continues on the same line:

    print("a")
    print("b")
    println()
    println("c")
    

    The output is:

    ab
    c
    

    Calling println() with nothing between the parentheses prints only a newline. Here it ends the line that the two print calls started, and calling it before a println would give you a blank line.

    Printing text and several things at once

    Text goes in double quotes, and the quotes are not printed. Inside a string, \n is an escape sequence that stands for a newline character, so one println can produce two lines:

    println("Line1\nLine2")
    
    Line1
    Line2
    

    To mix text and values in one line, put a $ before a variable's name inside the string, and Kotlin inserts the variable's value. This is called a string template. With a variable name holding "Andy":

    val name = "Andy"
    println("Hi, $name!")
    
    Hi, Andy!
    

    Variables get their own lesson later in this chapter, so treat this as a preview. The point for now is that one println call prints one line, and you decide what goes on it.

    What you can print

    println accepts values of any type and prints a text version of them:

    println(3.14)
    println(true)
    println('x')
    println(12 + 1)
    

    The output is:

    3.14
    true
    x
    13
    

    An expression such as 12 + 1 is evaluated first, and the result is printed.

    Common mistakes

    • Quoting a number you want to calculate with. "1998" is text, 1998 is a number.
    • Forgetting the parentheses. println 13 doesn't compile. A function is called with parentheses after its name.
    • Capital letters. Kotlin is case-sensitive. Println(13) fails with unresolved reference 'Println', because no function has that name.
    • Expecting print to start a new line. Consecutive print calls produce one long line.
    • Printing an array. println(intArrayOf(1, 2)) doesn't print the elements. It prints a type-and-hash text such as [I@6ea6d14e. Use intArrayOf(1, 2).toList() to see [1, 2]. A list can be printed directly.

    The exercise

    The lesson asks you to print the number 1998. There is one step: call println with the number between the parentheses, with no quotes, since it's a number.

    println(1998)
    

    Run it, and the console shows 1998.

    Key takeaways

    • The console shows a program's output, and printing is how you look at values while the program runs.
    • println(value) prints the value and then a newline, so the next output starts on a new line.
    • print(value) prints without the newline, and println() alone prints just a newline.
    • Numbers are written without quotes, and text is written in double quotes.
    • Function names are case-sensitive, and calls need parentheses.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free