Beginner 5 min readKotlin 2.0

Kotlin Raw Strings — Multi-Line Text with Triple Quotes

Raw strings (triple-quoted) let you write multi-line text and special characters without escape sequences. Perfect for SQL, JSON, and HTML.

What You Will Learn

  • Write multi-line raw strings
  • Use trimIndent() to clean indentation
  • Embed variables inside raw strings
  • When to use raw strings vs regular strings

What is a Raw String?

A raw string is delimited by triple double-quotes ("""). It can span multiple lines and contain special characters like \n, \t, and " without escaping.

Basic Raw String

kotlin
fun main() {
    val poem = """
        Roses are red,
        Violets are blue,
        Kotlin is great,
        And so are you!
    """.trimIndent()
    println(poem)
}
Output
Roses are red, Violets are blue, Kotlin is great, And so are you!

"""...""" is a raw string. .trimIndent() removes the common leading indentation from all lines. Without trimIndent(), the output would include leading spaces.

Beginner Tip: Always call .trimIndent() on raw strings to remove the indentation added for code readability.

Raw Strings for SQL and JSON

Raw strings are ideal for embedded SQL, JSON, HTML, or regex — no need to escape quotes or slashes.

JSON in a Raw String

kotlin
fun main() {
    val json = """
        {
            "name": "Kotlin",
            "version": 2.0,
            "isAwesome": true
        }
    """.trimIndent()
    println(json)
}
Output
{ "name": "Kotlin", "version": 2.0, "isAwesome": true }

No escaping needed for " inside a raw string. The JSON is clean and readable in the source code.

String Templates in Raw Strings

String templates ($variable and ${expression}) work inside raw strings too.

Template in Raw String

kotlin
fun main() {
    val name = "Juned"
    val score = 95
    val report = """
        Student: $name
        Score: $score
        Grade: ${if (score >= 90) "A" else "B"}
    """.trimIndent()
    println(report)
}
Output
Student: Juned Score: 95 Grade: A

$ and ${} work exactly the same inside raw strings as in regular strings.

Practice Exercise

Exercisepredict output

What does trimIndent() do to a raw string?

Quick Quiz

Quick Quiz

Which of these is valid inside a raw string without escaping?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review