Strings & String Templates
A string is a sequence of characters, and a string template lets you embed values directly inside text using $variable or ${'$ '} instead of clumsy concatenation.
Learn Strings & String Templates in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
You'll also meet triple-quoted multiline strings and the handful of string methods you'll reach for every day.
What You'll Learn in This Lesson
1️⃣ String Templates
Inside a normal "..." string, $name drops in a variable's value. For anything more complex — a calculation, a property, a method call — wrap it in ${'$ '} . This is cleaner and faster to read than joining strings with + .
2️⃣ Multiline Raw Strings
Triple quotes """...""" create a raw string: newlines are preserved and backslashes are literal, so there's nothing to escape. The one wrinkle is leading indentation from your source — .trimIndent() strips the common left margin so the text prints flush.
3️⃣ Common Methods & Character Access
Strings come with a rich toolbox. Every method returns a new string — the original never changes — which is why you can safely chain calls like s.trim().uppercase() .
You can also reach into individual characters with [ ] indexing, just like a list:
Your turn. Fill in the ___ blanks, then run and compare.
Split a lowercase full name and print each part capitalized. Combine split , indexing, and templates.
📋 Quick Reference — Strings
Practice quiz
Inside a normal double-quoted string, what does $name do?
- Inserts the value of the variable name
- Prints the literal text $name
- Throws a compile error
- Declares a new variable
Answer: Inserts the value of the variable name. $name is a string template that splices the variable's value into the text.
Which syntax inserts a whole expression like a method call into a template?
- $(expr)
- ${expr}
- lt;expr>
- $expr
Answer: ${expr}. Wrap any expression in curly braces: "${name.length}".
How do you print a literal dollar sign in a regular double-quoted string?
- $
- Use %d
- Escape it as \$
- It is impossible
Answer: Escape it as \$. Escape the dollar sign with a backslash: "Price: \$5".
What do triple quotes """...""" create?
- A char literal
- A mutable string
- A comment block
- A raw multiline string
Answer: A raw multiline string. Triple-quoted strings keep newlines and treat backslashes literally.
What does trimIndent() do to a triple-quoted string?
- Removes the common leading whitespace from every line
- Deletes all whitespace
- Reverses the string
- Converts it to uppercase
Answer: Removes the common leading whitespace from every line. trimIndent() strips the shared left margin so text prints flush.
Are Kotlin strings mutable?
- Yes, methods change them in place
- No, they are immutable and methods return new strings
- Only var strings are mutable
- Only inside a class
Answer: No, they are immutable and methods return new strings. Strings are immutable; uppercase(), trim(), etc. return brand-new strings.
What does " hi ".trim() return?
- " hi "
- "HI"
- "hi"
- An error
Answer: "hi". trim() removes leading and trailing whitespace, leaving "hi".
How do you read the first character of the String word?
- word.char(0)
- word.get(1)
- word.head
Index characters with [ ] (0-based), so word[0] is the first char.
What is the value of "Kotlin".length?
- 5
- 6
- 7
- 0
Answer: 6. The string "Kotlin" has six characters.
What does "a,b,c".split(",") produce?
- The string "abc"
split(",") breaks the text into a list of substrings: [a, b, c].