How do you put a literal dollar sign in a Kotlin string, including inside a raw (triple-quoted) string?
answer
- ${'$'} works everywhere
- \$ only in regular strings
- raw strings ignore backslash escapes
- common in regex with $ anchor
- bare $ before space is literal but fragile
basics
~10 sWrite ${'$'} to output a real dollar sign. In normal strings you can also use the escape $, but in triple-quoted strings backslashes are literal, so ${'$'} is the only way.
solid answer
~40 sA `$` starts a template, so to emit a literal dollar you must stop it from being interpreted. The portable, always-works technique is the template expression `${'$'}` — it evaluates the single `Char` literal `'$'` and inserts it, producing one dollar character. In a regular double-quoted string you may instead use the backslash escape `\$`. In raw (triple-quoted) strings backslash escapes are not processed — `\$` would print backslash-dollar — so `${'$'}` is the idiomatic and only clean way there. A standalone `$` not followed by an identifier-start character or `{` is also treated literally, but relying on that is fragile, so prefer `${'$'}` for clarity, e.g. `"""Price: ${'$'}9.99"""`.
code
kotlin · 4 linesval amount = 42
println("Pay ${'$'}$amount now") // Pay $42 now
println("Pay \$$amount now") // Pay $42 now (regular string only)
val pattern = """^[a-z]+${'$'}""" // raw string -> ^[a-z]+$go deeper
Knows you need some way to escape $ and that $ works in normal strings.
Knows ${'$'} is the universal escape and explains why $ fails in raw strings.
Connects this to real cases like embedding regexes ($ anchor) or shell variables in raw strings and chooses ${'$'} for portability.
Establishes a team convention (always ${'$'}) for consistency and reviews generated/templated code where dollar escaping is error-prone.
## The problem Because `$` introduces a template, you can't just type a dollar before a letter or `{` and expect a literal dollar — Kotlin will try to interpret it. You need a way to **escape** it. ## The universal solution: `${'$'}` The expression `${'$'}` is a template whose body is the **character literal** `'$'`. Evaluating it yields the single character `$`, which gets spliced into the string. This works **everywhere** — regular strings and raw strings alike — which is why it's the recommended idiom. ```kotlin val price = 9.99 println("Total: ${'$'}$price") // Total: $9.99 ``` Read it as: `${ ... }` is a template, and inside it the literal `'$'` char is the value. ## In regular (double-quoted) strings: `\$` also works In an ordinary string literal, backslash escapes are processed, so `\$` emits a literal dollar: ```kotlin println("Cost is \$5") // Cost is $5 ``` ## In raw / triple-quoted strings: `\$` does NOT work Raw strings (`"""..."""`) do **not** process backslash escapes — a backslash is just a backslash. So `\$` inside a raw string prints the two characters `\$`, not a dollar. Templates, however, *are* still active in raw strings, so `${'$'}` remains the correct escape: ```kotlin val regex = """\d+${'$'}""" // a regex meaning: digits at end-of-line println(regex) // \d+$ ``` This is a very common real-world case: writing regular expressions (where `$` means end-of-line anchor) or shell/templating snippets inside raw strings. ## A bare `$` is sometimes literal — but don't rely on it If a `$` is **not** followed by a valid identifier character or `{`, Kotlin leaves it literal: `"5$ each"` prints `5$ each` because the space after `$` can't start an identifier. This is brittle (a later edit could break it), so for clarity prefer `${'$'}`. ## Summary table - Regular string: `\$` or `${'$'}` - Raw string: `${'$'}` only (backslash is literal) - Bare `$` before non-identifier: literal, but fragile — avoid depending on it
- Why can't you use \$ inside a triple-quoted string?Raw strings don't process backslash escape sequences — the backslash is a literal character — so \$ would print backslash-dollar. Only ${'$'} works there.
- Why does ${'$'} produce a dollar?It's a template whose expression is the char literal '$'; evaluating it inserts that single character.
saying these in an interview costs you the question
- Claims \$ works in triple-quoted strings
- Doesn't know ${'$'} is the portable escape
- Thinks $$ escapes a dollar (it doesn't)
- Confuses char literal '$' with string "$"
- Believes you can't put a dollar sign in a raw string at all