Mastering Formatted Output with Kotlin's println Function
In the realm of programming, printing or outputting data to the console is a fundamental operation. Kotlin, a modern statically-typed programming language, provides the `println` function for this purpose. This function, however, offers more than just basic output. Let's delve into the world of formatted output using Kotlin's `println` function.
Understanding println in Kotlin
Kotlin's `println` function is an extension function of the `Any` class, which means it can be used with any object. It prints the given text to the standard output (usually the console) and then moves to the next line. The basic syntax is:
```kotlin println("Your text here") ```
Basic Formatting with println
Kotlin's `println` function supports basic formatting using placeholders. Placeholders are represented by `$` followed by a number or a name enclosed in curly braces. Here's how you can use them:

```kotlin val name = "Alice" val age = 30 println("Hi, $name! You are $age years old.") ```
Using Format Specifiers
Kotlin also supports format specifiers, allowing you to control the output format. The syntax is `$
d: Signed decimal integer.xorX: Hexadecimal integer (lowercase or uppercase).forF: Floating-point number (fixed or float).eorE: Scientific notation (lowercase or uppercase).s: String.
Here's an example:
```kotlin val pi = 3.14159 println("Pi is approximately $f") ```
Formatting with String Templates
Kotlin's string templates provide a more readable way to format strings. You can use curly braces `{}` to insert an expression into a string. If you need to format the output, you can use the `format` function:

```kotlin val name = "Bob" val age = 25 println("Hi, ${name.padEnd(10)}! You are ${age.toString().padStart(2)} years old.") ```
Controlling Output with print and println
While `println` prints the given text and then moves to the next line, `print` only prints the text without moving to the next line. This can be useful when you want to print multiple values on the same line:
```kotlin val x = 5 val y = 10 print("x = $x, y = $y") ```
Formatting with String Formatting Functions
Kotlin provides several string formatting functions that allow you to control the output format more precisely. These functions include `capitalize()`, `uppercase()`, `lowercase()`, `trim()`, `trimMargin()`, etc. Here's an example:
```kotlin val text = " hello, world! " println(text.trimMargin()) ```
Formatting with String Formatting Classes
Kotlin also provides classes for more advanced string formatting. The `StringBuilder` class is particularly useful when you need to build a string dynamically. Here's an example:

```kotlin val sb = StringBuilder() sb.append("Hello, ") sb.append("World!") println(sb.toString()) ```
Best Practices
When using `println` for formatted output, consider the following best practices:
- Use string templates for simple formatting.
- Use format specifiers for numerical data.
- Use `StringBuilder` for dynamic string construction.
- Use `print` when you want to print multiple values on the same line.
By following these best practices, you can make your code more readable and maintainable.










![[Tự học Kotlin] Hàm mở rộng trong Kotlin](https://i.pinimg.com/originals/4c/e3/ef/4ce3efccc6d4bb55379264da06d060c6.jpg)











