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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
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 ${'$'}.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Raw Strings and Triple Quotes” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Raw Strings and Triple Quotes”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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.
All lessons in this course
- String Basics: Literals, Escape Sequences & Length
- String Templates: $variable and ${expression}
- Raw Strings and Triple Quotes
- Key String Functions: trim, split, replace, contains