Kotlin Output
Displaying data and information in Kotlin programs
📺 Kotlin Output Functions
Learn how to display output in Kotlin using print functions. Master different ways to show data, format text, and create interactive console applications with various output techniques.
// Basic output in Kotlin
fun main() {
println("Hello, World!")
print("Welcome to Kotlin")
}
Output:
Welcome to Kotlin
Output Functions
println()
Print with new line
println("Hello")
println("World")
print()
Print without new line
print("Hello ")
print("World")
String Templates
Embed variables in output
val name = "Alice"
println("Hello, $name!")
Formatted Output
Control output formatting
val pi = 3.14159
println("Pi: %.2f".format(pi))
🔹 Basic Output Functions
Kotlin provides two main functions for console output:
🔸 println() - Print with New Line
fun main() {
println("First line")
println("Second line")
println("Third line")
// Print empty line
println()
println("After empty line")
}
Output:
Second line
Third line
After empty line
🔸 print() - Print without New Line
fun main() {
print("Hello ")
print("beautiful ")
print("world!")
println() // Add new line at the end
print("Numbers: ")
for (i in 1..5) {
print("$i ")
}
}
Output:
Numbers: 1 2 3 4 5
🔹 Printing Variables
Display different types of data:
fun main() {
// Different data types
val name = "Alice"
val age = 25
val height = 5.6
val isStudent = true
// Print variables
println("Name: $name")
println("Age: $age")
println("Height: $height feet")
println("Is student: $isStudent")
// Print multiple variables
println("$name is $age years old")
}
Output:
Age: 25
Height: 5.6 feet
Is student: true
Alice is 25 years old
🔹 String Templates and Expressions
Use string templates for dynamic output:
fun main() {
val x = 10
val y = 20
// Simple variable interpolation
println("x = $x, y = $y")
// Expression interpolation
println("Sum: ${x + y}")
println("Product: ${x * y}")
println("Average: ${(x + y) / 2.0}")
// Function calls in templates
val text = "kotlin"
println("Uppercase: ${text.uppercase()}")
println("Length: ${text.length}")
// Complex expressions
val numbers = listOf(1, 2, 3, 4, 5)
println("Numbers: $numbers")
println("Max: ${numbers.maxOrNull()}")
println("Sum: ${numbers.sum()}")
}
Output:
Sum: 30
Product: 200
Average: 15.0
Uppercase: KOTLIN
Length: 6
Numbers: [1, 2, 3, 4, 5]
Max: 5
Sum: 15
🔹 Formatted Output
Control the formatting of your output:
fun main() {
val pi = 3.14159265
val price = 29.99
val count = 42
// Format decimal places
println("Pi: %.2f".format(pi))
println("Pi (4 decimals): %.4f".format(pi))
// Format currency
println("Price: $%.2f".format(price))
// Format integers with padding
println("Count: %05d".format(count))
// Multiple values
println("Item: %s, Price: $%.2f, Qty: %d".format("Book", price, count))
// Using string templates (alternative)
println("Pi rounded: ${"%.3f".format(pi)}")
}
Output:
Pi (4 decimals): 3.1416
Price: $29.99
Count: 00042
Item: Book, Price: $29.99, Qty: 42
Pi rounded: 3.142
🔹 Multiline Output
Display multiple lines and formatted text:
fun main() {
val name = "John Doe"
val course = "Kotlin Programming"
val grade = 95
// Multiline string
val report = """
================================
STUDENT REPORT CARD
================================
Name: $name
Course: $course
Grade: $grade%
Status: ${if (grade >= 90) "Excellent" else "Good"}
================================
""".trimIndent()
println(report)
// Table-like output
println("%-10s %-15s %s".format("ID", "Name", "Score"))
println("%-10s %-15s %s".format("---", "----", "-----"))
println("%-10d %-15s %d".format(1, "Alice", 95))
println("%-10d %-15s %d".format(2, "Bob", 87))
println("%-10d %-15s %d".format(3, "Charlie", 92))
}
Output:
STUDENT REPORT CARD
================================
Name: John Doe
Course: Kotlin Programming
Grade: 95%
Status: Excellent
================================
ID Name Score
--- ---- -----
1 Alice 95
2 Bob 87
3 Charlie 92