Kotlin Academy · Lesson

String Basics: Literals, Escape Sequences & Length

Create strings, use escape characters, and measure string length.

Lesson 1 of 413 steps

String Basics: Literals, Escape Sequences & Length is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 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.

Strings Are Objects

In Kotlin, String is an immutable class with dozens of useful methods. Every string literal creates a String object.

String Literals

Strings are enclosed in double quotes. Single quotes are reserved for Char.
val greeting = "Hello, Kotlin!"
val empty = ""
val withSpaces = "  trimmed  "

Escape Sequences

Use backslash sequences for special characters: \n (newline), \t (tab), \" (quote), \\ (backslash).
println("Line 1\nLine 2")
println("Col1\tCol2")
println("She said \"hello\".")
println("Path: C:\\Users")

String Length

The length property returns the number of characters in a string.
val name = "Kotlin"
println(name.length)      // 6
println("".length)        // 0
println("  hi  ".length) // 6 (spaces count)

Accessing Characters by Index

Use bracket notation or .get(index) to access individual characters. Kotlin strings are zero-indexed.
val s = "Hello"
println(s[0])         // H
println(s[4])         // o
println(s.first())    // H
println(s.last())     // o

isEmpty and isBlank

isEmpty() returns true for "". isBlank() returns true for empty or whitespace-only strings.
println("".isEmpty())         // true
println("  ".isEmpty())       // false
println("  ".isBlank())       // true
println("hi".isBlank())       // false

String Comparison

Use == for structural (value) equality and === for reference equality. In Kotlin, == on strings compares content.
val a = "hello"
val b = "hello"
println(a == b)           // true (content)
println(a.equals(b, ignoreCase = true)) // true

Substring

substring(start, end) extracts a portion of a string. The end index is exclusive.
val text = "Hello, Kotlin!"
println(text.substring(7))      // Kotlin!
println(text.substring(0, 5))   // Hello

indexOf and contains

indexOf returns the first position of a substring (-1 if not found). contains checks membership.
val s = "Hello, Kotlin!"
println(s.indexOf("Kotlin"))   // 7
println(s.contains("Hello"))   // true
println("Kotlin" in s)        // true (in operator)

uppercase, lowercase, capitalize

String case conversion functions are simple extensions.
val s = "hello kotlin"
println(s.uppercase())          // HELLO KOTLIN
println(s.lowercase())          // hello kotlin
println(s.replaceFirstChar { it.uppercase() })

Strings Are Immutable

Every string operation returns a NEW string — the original never changes. This is safe but means building large strings in loops should use StringBuilder.
val s = "hello"
val upper = s.uppercase()  // new string
println(s)     // still "hello"
println(upper) // "HELLO"

Quick Check

What does isBlank() return for the string " " (three spaces)?

Recap

Strings are immutable objects with length, index access, comparison, and case functions. Next: string templates and embedding expressions!
Free to start

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 “String Basics: Literals, Escape Sequences & Length” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “String Basics: Literals, Escape Sequences & Length”, 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 “String Basics: Literals, Escape Sequences & Length”?

Create strings, use escape characters, and measure string length. 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 1 of 4.

How long does the “String Basics: Literals, Escape Sequences & Length” 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

  1. String Basics: Literals, Escape Sequences & Length
  2. String Templates: $variable and ${expression}
  3. Raw Strings and Triple Quotes
  4. Key String Functions: trim, split, replace, contains
← Back to Kotlin Academy