Raw Strings and Triple Quotes
Use triple-quoted strings for multiline text and raw content without escapes.
Raw Strings and Triple Quotes is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Raw String?
A raw string uses triple quotes """...""" and preserves text exactly as written — no escape sequences, no special characters.
Basic Raw String
Triple-quoted strings can span multiple lines without explicit \n.
fun main() {
val text = """
Line 1
Line 2
Line 3
"""
println(text)
}No Escape Sequences Needed
Inside a raw string, backslashes and quotes appear literally. Perfect for regex patterns and Windows paths.
fun main() {
val regex = """\d+\.\d+"""
val winPath = """C:\Users\Alice\Documents"""
println(regex)
println(winPath)
}Multiline JSON
Raw strings are perfect for embedded JSON, SQL, or HTML literals — no escape clutter.
fun main() {
val json = """
{
"name": "Kotlin",
"year": 2011
}
""".trimIndent()
println(json)
}trimIndent()
trimIndent() removes the common leading whitespace from every line, letting you indent the source nicely while keeping clean output.
fun main() {
val poem = """
Roses are red
Violets are blue
Kotlin is sweet
And so are you
""".trimIndent()
println(poem)
}trimMargin()
trimMargin() removes a leading margin marker (default |). Use when explicit alignment beats indent matching.
fun main() {
val msg = """
|Hello,
| World!
|Goodbye.
""".trimMargin()
println(msg)
}Custom Margin Character
Pass any character to trimMargin to customize the marker.
fun main() {
val text = """
>Line A
>Line B
""".trimMargin(">")
println(text)
}String Templates Still Work
Raw strings still interpolate $variable and ${expression}. Only escape sequences are turned off.
fun main() {
val name = "Kotlin"
val version = 1.9
val report = """
Name: $name
Version: $version
Build: ${if (version >= 1.0) "stable" else "beta"}
""".trimIndent()
println(report)
}Embedding Literal Quotes
Double quotes inside triple-quoted strings need no escaping — write them as-is.
fun main() {
val quote = """She said, "Hello!" and smiled."""
println(quote)
}Embedding the Dollar Sign Literally
To put a literal $ inside a raw string, use the template trick: ${'$'}.
fun main() {
val price = """Cost is ${'$'}100"""
println(price) // Cost is $100
}Practical: SQL Query
Triple-quoted strings keep long SQL queries readable in source.
fun main() {
val table = "users"
val sql = """
SELECT id, name, email
FROM $table
WHERE active = true
ORDER BY name
""".trimIndent()
println(sql)
}Quick Check
Which function removes the common leading whitespace from each line of a raw string?
Recap
Triple-quoted strings preserve text literally (no escapes) and span multiple lines. Use trimIndent() or trimMargin() to clean up indentation. Templates still work; literal $ needs ${'$'}.
Frequently asked questions
Is the “Raw Strings and Triple Quotes” lesson free?
Yes — the full text of “Raw Strings and Triple Quotes” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
What will I learn in “Raw Strings and Triple Quotes”?
Use triple-quoted strings for multiline text and raw content without escapes. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Raw Strings and Triple Quotes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.