Kotlin Output (Print Text)

Welcome to The Coding College, your one-stop solution for learning coding and programming. In this article, we’ll cover the fundamentals of Kotlin Output—how to display text on the screen using Kotlin. Understanding how to output text is the foundation of learning Kotlin, making it an essential starting point for beginners.

Why Learn Kotlin Output?

Printing text is the first step in interacting with your program. Kotlin offers simple and intuitive ways to output data, whether you’re debugging or providing user information. Here’s what you’ll learn in this tutorial:

  • Printing plain text.
  • Using variables in output.
  • Formatting output for better readability.

The Basics: print() and println()

Kotlin provides two main functions for output:

  • print()
    Displays text on the same line without adding a new line.
print("Hello, Kotlin!")  
print(" Welcome to The Coding College.")  
  • Output:
Hello, Kotlin! Welcome to The Coding College.
  • println()
    Adds a new line after the text.
println("Hello, Kotlin!")  
println("Welcome to The Coding College.")  
  • Output:
Hello, Kotlin!  
Welcome to The Coding College.

Printing Variables in Kotlin

You can print variables directly by embedding them into text.

Using String Interpolation

Kotlin allows embedding variables within strings using the $ symbol:

val language = "Kotlin"  
val platform = "The Coding College"  
println("Welcome to $platform! Let's learn $language.")  

Output:

Welcome to The Coding College! Let's learn Kotlin.

Expression Evaluation

You can include expressions inside ${}:

val number = 10  
println("The square of $number is ${number * number}.")  

Output:

The square of 10 is 100.

Formatting Output in Kotlin

For more complex formatting, Kotlin supports templates and string manipulation.

Example: Aligning Text

val name = "Alice"  
val score = 95  
println("Name: $name, Score: $score")  

Output:

Name: Alice, Score: 95

Multi-Line Strings

Use triple quotes (""") for multi-line text:

val message = """  
    Kotlin is easy to learn.  
    It’s the official language for Android development.  
"""  
println(message)  

Output:

Kotlin is easy to learn.  
It’s the official language for Android development.  

Practical Examples

Example 1: Printing a Welcome Message

fun main() {  
    println("Welcome to The Coding College!")  
    println("Your journey with Kotlin begins here.")  
}

Output:

Welcome to The Coding College!  
Your journey with Kotlin begins here.

Example 2: Displaying User Input

fun main() {  
    val name = "Naman"  
    println("Hello, $name! Welcome to Kotlin.")  
}

Output:

Hello, Naman! Welcome to Kotlin.

Example 3: Using a Loop to Print Numbers

fun main() {  
    for (i in 1..5) {  
        println("Number: $i")  
    }  
}

Output:

Number: 1  
Number: 2  
Number: 3  
Number: 4  
Number: 5

Key Points to Remember

  • Use print() for continuous output on the same line.
  • Use println() to add a new line after each output.
  • Leverage string interpolation for dynamic text.
  • Multi-line strings and formatting enhance readability.

Learn More at The Coding College

Mastering output is just the beginning. At The Coding College, we provide step-by-step guides to help you excel in Kotlin and other programming languages.

Visit The Coding College for more Kotlin tutorials and programming resources. Whether you’re a beginner or an advanced programmer, we’ve got something for you!

Conclusion

Printing text in Kotlin is straightforward yet powerful, enabling you to interact effectively with your programs. Use this guide to solidify your understanding of Kotlin’s output methods.

Leave a Comment