📤 Kotlin – Output: How to Display Data in Kotlin
🧲 Introduction – Why Learn Kotlin Output?
Displaying output is one of the most basic yet essential parts of programming. In Kotlin, output functions help developers debug, interact with users, and format results. Kotlin makes output handling clean and intuitive using standard print()
and println()
functions.
🎯 In this guide, you’ll learn:
- The difference between
print()
andprintln()
in Kotlin - How to use string templates for clean formatting
- Examples of printing variables, expressions, and multiline content
- Output best practices and common mistakes
📄 Basic Output in Kotlin
✅ println()
– Print with Newline
fun main() {
println("Welcome to Kotlin!")
}
🟢 Output:
Welcome to Kotlin!
println()
prints the string and moves the cursor to the next line.
✅ print()
– Print Without Newline
fun main() {
print("Kotlin ")
print("is ")
print("fun!")
}
🟢 Output:
Kotlin is fun!
print()
displays the output without adding a newline.
🧩 Printing Variables and Expressions
fun main() {
val name = "Riya"
val age = 22
println("Name: $name, Age: $age")
}
🟢 Output:
Name: Riya, Age: 22
✅ Use $variableName
for inline values
✅ Use ${expression}
for calculations
val x = 5
val y = 10
println("Sum is: ${x + y}")
🟢 Output:
Sum is: 15
🧵 Multiline Output with Triple Quotes
fun main() {
val message = """
Kotlin Output Demo:
1. println()
2. print()
""".trimIndent()
println(message)
}
🟢 Output:
Kotlin Output Demo:
1. println()
2. print()
💡 Use trimIndent()
to remove unwanted indentation in multiline strings.
🖋️ Output Formatting Best Practices
Task | Example |
---|---|
🔹 Print numbers | println(100) |
🔹 Print calculation result | println("Total: ${5 * 10}") |
🔹 Newline between prints | Use println() |
🔹 No newline | Use print() |
🔹 Multiline output | Use triple quotes """...""" |
🚫 Common Output Mistakes
❌ Mistake | ✅ Correct Syntax |
---|---|
println("Hello) | println("Hello") |
println('A') (wrong quotes) | println("A") or println('A') |
println(x + y) when not defined | Declare variables before printing |
📌 Summary – Recap & Next Steps
Kotlin’s print()
and println()
functions make output handling easy and readable. Whether you’re displaying simple text, debugging values, or formatting structured output, Kotlin provides the tools to do it effectively.
🔍 Key Takeaways:
println()
adds a newline;print()
doesn’t.- Use string templates to embed variables and expressions.
- Triple quotes (
"""
) support multiline output.
⚙️ Practical Use:
Kotlin’s output features are ideal for CLI tools, logging messages, debugging, or simply interacting with users during runtime.
❓ FAQs – Kotlin Output
❓ What is the difference between print()
and println()
?
✅ print()
displays output on the same line, while println()
adds a newline after the output.
❓ How do I include variable values in a print statement?
✅ Use Kotlin’s string template:
val name = "Amit"
println("Hello, $name!")
❓ Can I print expressions directly in Kotlin?
✅ Yes. Use ${expression}
syntax:
println("2 + 3 = ${2 + 3}")
❓ How do I print multiline text in Kotlin?
✅ Use triple-quoted strings (""" ... """
) along with trimIndent()
or trimMargin()
.
❓ Do I need semicolons after print statements in Kotlin?
✅ No. Kotlin doesn’t require semicolons at the end of statements.
Share Now :