Kotlin print() – Print without New Line
To print text in Kotlin without automatically moving to a new line, use print(). It writes the supplied value to the standard output and leaves the next output at the current position.
This is the main difference between print() and println(): print() does not append a line break, while println() does.
Kotlin print() syntax for output without a newline
The basic syntax is:
print(value)
value can be text, a number, a character, a Boolean value, a variable, or an expression. Kotlin converts the value to its text representation before writing it to standard output.
Example 1 – Kotlin print() – String message
In this example, we shall print a string to the standard output console using print().
Kotlin Program – example.kt
/**
* Kotlin - print() - Print without New Line
*/
fun main(args: Array<String>) {
var str = "Hello World"
print(str)
print(str)
}
Run the above Kotlin program, and you shall see the following output print to standard console output.
Output
Hello WorldHello World
Please note that, after printing the message “Hello World”, the cursor waits right after the message. When another print() statement is executed, the message is printed from the current cursor position. So, the messages are printed right after one another.
Kotlin print() and println() compared
Use print() when several values should appear on the same line. Use println() when the next output should begin on a new line.
fun main() {
print("Hello")
print(" Kotlin")
println("!")
println("Next line")
}
Output
Hello Kotlin!
Next line
The first two print() calls continue on the same line. The following println() prints its argument and then terminates that line.
Add spaces between values while using Kotlin print()
print() does not insert spaces between separate calls. If you want a space, comma, or other separator, include it explicitly.
fun main() {
print("Kotlin")
print(" ")
print("Programming")
}
Output
Kotlin Programming
This is useful when you want exact control over how values are arranged on one output line.
Use \n when a Kotlin print() call needs an explicit line break
Although print() does not add a newline automatically, the text you print can contain the newline escape sequence \n. This lets you choose exactly where a line break occurs.
fun main() {
print("First line\nSecond line")
}
Output
First line
Second line
The newline here comes from the string itself, not from print().
Example – Kotlin print() – Other Datatypes
In this example, we shall print messages of different datatypes to the standard output console using print() statement.
Kotlin Program – example.kt
/**
* Kotlin - print() - Print without New Line
*/
fun main(args: Array<String>) {
print("Hello World") //string
print(35) //int
print(12345L) //long
print(0b00001011) //byte
print(2.35) //double
print(41.253F) //float
print('m') //Char
print(true) //Boolean
}
Run the above Kotlin program, and you shall see the following output print to standard output.
Output
Hello World3512345112.3541.253mtrue
Each value is written using its text representation, and no separator is added between calls. The suffix used in source code for a numeric literal, such as L or F, is not part of the printed value. Also, 0b00001011 is an integer binary literal with the decimal value 11; despite the comment in the sample, it is not a Byte unless it is explicitly converted or stored as one.
Print variables and expressions on the same line in Kotlin
You can pass variables and expressions directly to print(). String templates are often convenient when several values need to be combined into readable output.
fun main() {
val name = "Asha"
val score = 92
print("Student: $name, ")
print("Score: ${score + 3}")
}
Output
Student: Asha, Score: 95
Kotlin print() requires an argument
No. Unlike println(), which can be called as println() to output only a line break, print() requires a value to print. If your goal is simply to move to the next line, use println().
Kotlin print() without newline: key points
print(value)writes the value without appending a newline.- Consecutive
print()calls continue from the current output position. - Add spaces or separators explicitly when values should not run together.
- Use
\ninside a string when you need a line break at a specific position. - Use
println()when the line should end automatically after the value is printed.
In this Kotlin Tutorial, we learned how to print a message without new line to the standard output.
TutorialKart.com