Output with print and println

println vs print

Example

println() prints and then moves to a new line. print() stays on the same line.

fun main() {
    print("Swiggy ")
    print("Zomato ")
    println("Zepto")
    println("Next line")
    println()            // empty line
    println("After an empty line")
}

Output

Swiggy Zomato Zepto
Next line

After an empty line

Printing Numbers and Expressions

Example

You can print numbers and calculations directly. Use string templates (covered later) to mix text and values.

fun main() {
    println(42)
    println(3.14)
    println(10 + 5)
    println("10 + 5")        // text, not a calculation
    println("Total: " + 15)  // + joins a string and a number
    println("Total: ${10 + 5}") // template, preferred in Kotlin
}

Output

42
3.14
15
10 + 5
Total: 15
Total: 15

Escape Characters

Example

Special characters start with a backslash inside a string.

fun main() {
    println("Name:\tAsha")
    println("Line 1\nLine 2")
    println("She said \"Hi\"")
    println("Path: C:\\Users\\asha")
    println("Price: \$5")
}

Output

Name:	Asha
Line 1
Line 2
She said "Hi"
Path: C:\Users\asha
Price: $5
EscapeMeaning
\nnew line
\ttab
\"double quote
\\backslash
\$dollar sign (otherwise it starts a template)