0
0
KotlinHow-ToBeginner · 3 min read

How to Format String in Kotlin: Simple Guide with Examples

In Kotlin, you can format strings using string templates with ${variable} syntax or the String.format() function for more complex formatting. String templates are simple and readable, while String.format() allows printf-style formatting.
📐

Syntax

Kotlin provides two main ways to format strings:

  • String templates: Use ${variable} or ${expression} inside a string to insert values.
  • String.format(): Use this function with format specifiers like %s, %d, %f to format strings similar to Java's printf.
kotlin
val name = "Alice"
val age = 30

// String template
val greeting = "Hello, $name! You are $age years old."

// String.format
val formatted = String.format("Hello, %s! You are %d years old.", name, age)
💻

Example

This example shows how to use string templates and String.format() to create formatted strings with variables.

kotlin
fun main() {
    val name = "Bob"
    val score = 95.5

    // Using string template
    val message1 = "Player $name scored $score points."

    // Using String.format for 2 decimal places
    val message2 = String.format("Player %s scored %.2f points.", name, score)

    println(message1)
    println(message2)
}
Output
Player Bob scored 95.5 points. Player Bob scored 95.50 points.
⚠️

Common Pitfalls

Common mistakes when formatting strings in Kotlin include:

  • Forgetting to use $ in string templates, which results in the variable name being printed literally.
  • Using String.format() without matching format specifiers and arguments, causing runtime errors.
  • Not specifying precision for floating-point numbers in String.format(), leading to too many decimal places.
kotlin
fun main() {
    val name = "Eve"
    val score = 88.1234

    // Wrong: missing $ in template
    val wrongTemplate = "Player name scored $score points."

    // Right: use $name
    val rightTemplate = "Player $name scored $score points."

    // Wrong: format specifier mismatch
    // val wrongFormat = String.format("Player %d scored %f points.", name, score) // Error

    // Right: correct specifiers
    val rightFormat = String.format("Player %s scored %.2f points.", name, score)

    println(wrongTemplate)
    println(rightTemplate)
    println(rightFormat)
}
Output
Player name scored 88.1234 points. Player Eve scored 88.1234 points. Player Eve scored 88.12 points.
📊

Quick Reference

MethodUsageExample
String TemplateInsert variables with $variable or ${expression}"Hello, $name!"
String.format()Use printf-style format specifiersString.format("%.2f", 3.14159)
Format Specifiers%s = string, %d = integer, %f = float/double%d, %s, %.2f

Key Takeaways

Use string templates with $variable for simple and readable formatting.
Use String.format() for precise control like decimal places and padding.
Always match format specifiers with argument types in String.format().
Remember to prefix variables with $ in string templates to insert values.
Use %.2f in String.format() to limit floating-point decimals.