Kotlin is a statically typed programming language that runs on the Java Virtual Machine (JVM). It was developed by JetBrains and is now an official language for Android development. Kotlin is designed to be concise, expressive, and safe. It is also interoperable with Java, which means that you can use Kotlin and Java code together in the same project.
Kotlin Syntax & Output (Print Text) (Lesson 2 PDF)
Overview about the Lesson
In this lesson, you will learn about printing text in Kotlin. You will learn how to use the println() and print() functions to output values and print text. You will also learn how to format strings using string templates.


The Content of the Lesson
The lesson is divided into two parts. In the first part, you will learn how to use the println() and print() functions to output values and print text. You will also learn how to format strings using string templates. In the second part, you will learn how to use the readLine() function to read input from the user.
Why This Lesson
This lesson is designed for programmers who are new to Kotlin and want to learn how to print text and read input from the user. It is also suitable for programmers who are familiar with Java and want to learn Kotlin.
Kotlin Output in Practice: println, print and String Templates
This lesson’s PDF covers Kotlin’s syntax rules and how to print text. Here is a hands-on summary with runnable examples.
println() vs print()
fun main() {
print("Hello ")
print("World")
println() // just moves to a new line
println("Line 1")
println("Line 2")
}Output:
Hello World
Line 1
Line 2print() keeps the cursor on the same line; println() adds a line break after the text.
Printing numbers and calculations
fun main() {
println(42)
println(3 + 4 * 2) // 11, multiplication first
println(10 / 4) // 2, whole-number division
println(10.0 / 4) // 2.5
}Note that dividing two whole numbers gives a whole number. Use a decimal (10.0) to get a decimal result.
String templates: mixing text and values
fun main() {
val item = "Laptop"
val price = 950
println("The $item costs $price dollars")
println("Two of them cost ${price * 2}")
}Use $variable for a simple variable and ${expression} for any calculation or property, such as ${item.length}.
Special characters
| Escape | Meaning | Example output |
|---|---|---|
| n | New line | Splits text over two lines |
| t | Tab | Adds horizontal space |
| “ | Double quote | Prints a quote inside a string |
| \ | Backslash | Prints a single |
| $ | Dollar sign | Prints $ instead of starting a template |
Multi-line strings
fun main() {
val menu = """
1. New game
2. Load game
3. Exit
""".trimIndent()
println(menu)
}Triple-quoted strings keep line breaks exactly as typed, and trimIndent() removes the common leading spaces.
Kotlin syntax rules to remember
- Every program starts in
fun main(). - Semicolons are optional; one statement per line is the norm.
- Kotlin is case-sensitive:
printlnworks,PrintLndoes not. - Code blocks use curly braces
{ }; indentation is for readability only.
Practice exercise
Print a small receipt with an item name, a quantity of 3 and a unit price of 12.5, and calculate the total with a string template.
fun main() {
val item = "Notebook"
val qty = 3
val unitPrice = 12.5
println("Item: $item")
println("Total: ${qty * unitPrice}") // Total: 37.5
}Frequently Asked Questions
How do I print a dollar sign in Kotlin?
Escape it with a backslash: println("Price: $5"). A $ not followed by a valid identifier also prints as is.
Can I format numbers to two decimal places?
Yes: println("%.2f".format(37.456)) prints 37.46.
Continue the course
← Previous: Lesson 1: Getting Started | Next: Lesson 3: Comments & Variables →
- Lesson 1: Getting Started
- Lesson 2: Syntax & Output (you are here)
- Lesson 3: Comments & Variables
- Lesson 4: Data Types
- Lesson 5: Operators
- Lesson 6: Set Operations & Grouping
- Lesson 7: Booleans & If…Else
- Lesson 8: When, While, Break & Continue
- Lesson 9: Arrays
- Lesson 10: For Loops & Ranges
- Lesson 11: Functions & Classes
- Lesson 12: Classes, Objects & Constructors
- Lesson 13: Class Functions & Inheritance
Conclusion
Printing text and reading input are essential skills for any programmer. In this lesson, you learned how to use the println() and print() functions to output values and print text. You also learned how to format strings using string templates and how to read input from the user using the readLine() function.
Download From Link
You can download the lesson from the following link:














