skip to content

Inside a raw string, how do you include a literal $ and a literal sequence of three double quotes, given templates still expand?

level: seniorimportance: should knowfreq 30%

answer

  1. Templates expand in raw strings
  2. Literal $ via ${'$'}
  3. Three quotes via ${"\"\"\""}
  4. Backslash-escape of $ does NOT work in raw strings
  5. Everything else is verbatim

basics

~20 s

Templates run even in raw strings, so a bare $ before a name becomes a variable. Write a literal dollar with ${'$'}. Three quotes are hard to embed; use a template like ${"""""} to insert them.

solid answer

~50 s

Raw strings skip escape processing but still **expand templates** (`$name`, `${expr}`). That creates two awkward cases. (1) A literal dollar sign: a bare `$` followed by a letter/identifier is parsed as a template, so to emit a literal `$` you write `${'$'}` — a template whose value is the Char `'$'`. For `$` followed by non-identifier characters it is often kept literally, but `${'$'}` is the reliable form. (2) A literal run of three double quotes can't appear directly because `"""` closes the literal; you inject them through a template, e.g. `${"\"\"\""}`, where the inner is a regular escaped string. These are the only characters you can't write verbatim in a raw string; everything else (backslashes, single/double quotes, newlines) is literal. This matters when generating code, regex with `$` anchors, JSON with currency, or shell snippets.

code

kotlin · 10 lines
kotlin
val n = 5
val s = """
price=${'$'}$n
delim=${"\"\"\""}
backslash=\$n   // literal backslash, then template $n -> 5
""".trimIndent()
println(s)
// price=$5
// delim="""
// backslash=\5

go deeper

for a junior

May know ${'$'} for a dollar sign but is shaky on why and on the three-quote case.

for a middle

Explains template expansion and the ${'$'} idiom confidently.

for a senior

Handles both edge cases, knows $ doesn't escape, and connects to regex/codegen use cases.

for a principal

Weighs raw-string codegen readability versus templating libraries and sets conventions to avoid template footguns.

## The principle A raw string `"""..."""` does **no escape processing** but **does** perform **string-template substitution**. Templates come in two forms: - `$identifier` — substitutes a variable's value. - `${expression}` — evaluates and substitutes any expression. Because this still happens, the dollar sign and the triple-quote delimiter become special. ## Literal dollar sign If you write `$x`, Kotlin treats `x` as a variable reference. To output a literal `$`, escape it through a template that yields the character: ```kotlin val price = """Total: ${'$'}9.99""" // Total: $9.99 ``` `${'$'}` is a template whose expression is the Char literal `'$'`. (A bare `$` immediately followed by whitespace or a non-identifier char is usually kept literal, but relying on that is fragile; `${'$'}` always works — useful for regex end-anchors like `${'$'}` too.) ## Literal three double quotes You cannot type `"""` inside the literal because it ends it. Inject it via a template containing a regular (escaped) string: ```kotlin val q = """He said: ${"\"\"\""}""" // He said: """ ``` Here `"\"\"\""` is an ordinary string of three quotes (each `\"` is one quote), and the template drops it into the raw string. A single or double pair of quotes is fine to write directly; only the closing-delimiter sequence needs this trick. ## What is always literal Everything else is verbatim: `\` (backslash), single `"`, `'`, tabs you actually press, and real newlines. So `"""a"b\c"""` is the five characters `a " b \ c`. ## Why it matters - **Regex** anchors: end-of-line `$` written as `${'$'}` inside `Regex("""...${'$'}""")`. - **Money/JSON/templated code** containing dollars. - **Code generators** that must emit Kotlin/Groovy raw strings (the triple-quote case). ## Common mistakes - Doubling backslashes out of habit (`\\d`) — wrong in raw strings; use `\d`. - Forgetting that `$name` interpolates and accidentally referencing a variable. - Trying to backslash-escape the dollar (`\$`) — in a raw string `\$` is literally backslash then a template start, not an escaped dollar.

  • Does `\$` escape the dollar in a raw string?
    No. There is no escape processing, so `\$` is a literal backslash followed by a template start. Use ${'$'} for a literal dollar.
  • Can you put a single or double quote directly in a raw string?
    Yes — one or two quotes are fine verbatim. Only the three-in-a-row closing sequence needs the template workaround.

saying these in an interview costs you the question

  • Saying templates do not run in raw strings
  • Using \$ to escape a dollar
  • Claiming you can type """ directly inside the literal
  • Doubling backslashes inside a raw string
  • Thinking ${'$'} prints the literal text dollar-brace

context