What you'll learn
Text is everywhere in programming. This module makes you fluent with Kotlin's excellent string support — templates, multiline strings, and methods — and shows how to read and safely parse input from the user.
By the end you'll be able to:
- Build strings cleanly with templates
- Write multiline (raw) strings with
trimIndent() - Transform text with the essential string methods
- Read input with
readln()and parse it safely
String templates
You met templates in Module 2. To recap: $name inserts a variable, and ${...} inserts any expression — a calculation, a property, or a method call. They're far cleaner than concatenating with +:
val name = "Sara"
val age = 25
println("$name is $age years old") // simple variables
println("In 5 years: ${age + 5}") // an expression
println("Shout: ${name.uppercase()}") // a method callMultiline strings
Triple quotes create a raw string that spans multiple lines and ignores escape sequences — ideal for JSON, SQL, or formatted messages. Pair it with trimIndent() to remove the leading indentation you added for readability:
val message = """
Dear Sara,
Welcome to Kotlin!
Best regards.
""".trimIndent()
println(message)String methods
Kotlin strings are immutable and come with a rich set of methods, each returning a new string:
| Method | What it does |
|---|---|
.trim() | remove surrounding whitespace |
.uppercase() / .lowercase() | change case |
.split(sep) | break into a list |
.replace(a, b) | replace every a with b |
.contains(x) | test for a substring |
.startsWith(x) / .endsWith(x) | test the start/end |
.length | number of characters (a property) |
val text = " Hello, Kotlin "
println(text.trim()) // "Hello, Kotlin"
println("kotlin".uppercase()) // KOTLIN
println("a,b,c".split(",")) // [a, b, c]
println("Kotlin".replace("K", "C")) // Cotlin
println("Kotlin".contains("lin")) // true
println("Kotlin".length) // 6Reading input
Read a line from the console with readln(), which returns a String. As always, convert it if you need a number:
print("What's your name? ")
val name = readln() // reads a line as a String
print("Your age? ")
val age = readln().toInt() // convert to Int
println("Hi $name, next year you'll be ${age + 1}")Parsing safely
What if the user types something that isn't a number? toInt() would crash. Kotlin's null-aware companion, toIntOrNull(), returns null instead — which you handle cleanly with the Elvis operator from Module 3. This is null safety paying off in everyday code:
val input = "42x"
// toIntOrNull() returns null instead of crashing on bad input
val number = input.toIntOrNull()
println(number ?: "Not a valid number")
println("100".toIntOrNull() ?: -1) // 100Tip
...OrNull() variants (toIntOrNull, toDoubleOrNull) for any input you don't control. They turn "might crash" into "might be null," which the compiler then makes you handle.Recap & quick check
Key takeaways
- String templates: $name for a variable, ${...} for any expression.
- Triple-quoted raw strings span lines and ignore escapes; trimIndent() cleans up indentation.
- String methods (trim, split, replace, uppercase…) return new strings; .length is a property.
- readln() reads a line as a String; convert with toInt()/toDouble() when needed.
- Use toIntOrNull() for untrusted input — it returns null instead of crashing.
Quick check
1. How do you insert an expression into a string?
2. What creates a multiline string in Kotlin?
3. What does readln() return?
4. What does toIntOrNull() do on invalid input like "42x"?
5. String methods like uppercase() return…
Excellent — you've completed Phase 1! You can store data, make decisions, loop, write functions, and handle text and input — all with Kotlin's signature safety. Next comes Phase 2 — Functions & Functional Programming.