← Back to Kotlin Course | Chapter 1: Setup & Basics | Lesson 6 of 7

print() vs println()

print() and println() are two ways to show text on screen; one leaves the cursor right after the text, and the other moves it to a new line.
Syntax
kotlin
print(value)      // no newline
println(value)    // adds a newline

Using print()

print() writes text to standard output without adding a newline afterward, so the next thing printed continues right where the previous output left off.

Example: Using print()

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Hello, " to the console
    print("Hello, ")
    // Print "World!" to the console
    print("World!")
    // Print " No newline between these." to the console
    print(" No newline between these.")
}

Using println()

println() behaves like print() but appends a newline character at the end, so each call starts fresh on the next line of output.

Example: Using println()

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "First line" to the console, with a trailing newline
    println("First line")
    // Print "Second line" to the console, with a trailing newline
    println("Second line")
    // Print "Third line" to the console, with a trailing newline
    println("Third line")
}

Printing Non-String Values

Both print() and println() accept numbers, booleans, and other types directly -- Kotlin converts them to their string representation automatically before printing.

Example: Printing Non-String Values

markup
// Entry point: execution of the program starts here
fun main() {
    // Print `42` to the console, with a trailing newline
    println(42)
    // Print `3.14` to the console, with a trailing newline
    println(3.14)
    // Print `true` to the console, with a trailing newline
    println(true)
}

Mixing print() and println()

Combining both lets you build a single line piece by piece with print() and then finish it with a println() call, which is useful for formatting output that has multiple parts.

Note: Overusing print() for multi-part lines can get confusing -- string templates (covered later) are often clearer.

Example: Mixing print() and println()

markup
// Entry point: execution of the program starts here
fun main() {
    // Print "Score: " to the console
    print("Score: ")
    // Print `95` to the console
    print(95)
    // Print "/100" to the console, with a trailing newline
    println("/100")
    // Print "Done!" to the console, with a trailing newline
    println("Done!")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using print() repeatedly and being surprised that output runs together on one line without spaces or newlines.
  2. Forgetting to add a space or separator manually when chaining several print() calls together.
  3. Assuming println() with no arguments does nothing, when it actually prints just a newline.
Chapter Summary
  • print() outputs text with no trailing newline; the next output continues on the same line.
  • println() outputs text followed by a newline, moving subsequent output to the next line.
  • Both functions can print numbers, booleans, and other types by converting them to their string form automatically.
  • println() called with no arguments simply prints an empty line.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.