In this tutorial, you shall learn how to print a given string in Kotlin, using print() or println() functions, with examples.
Kotlin – Print a string
To print a string in Kotlin, you can use print() or println() function.
print() function prints the given string argument to standard console output.
println() function prints the given string argument to standard console output with a trailing new line.
Syntax
If str
is the given string, then the syntax to print the string to standard output is
print(str)
//or
println(str)
Examples
1. Print string using print() function
In the following program, we take a string in str
, and print it to output using print() function.
Main.kt
fun main() {
val str = "hello world"
print(str)
}
Output
hello world
2. Print string using println() function
In the following program, we take a string in str
, and print it to output using println() function. Please note that this function prints the given string, and then prints a new line.
Main.kt
fun main() {
val str = "hello world"
println(str)
}
Output
hello world
Conclusion
In this Kotlin Tutorial, we learned how to print a string to standard output using print() or println() functions.