CodeBlanks Logo Icon CodeBlanks

String

Learn the String type and its methods.

String

A String is a sequence of zero or more characters. It is written between double quotes: "Hello". In Kotlin, strings are immutable; once created, their content does not change.

Common operations: length, uppercase(), substring(b, e).

val s = "Kotlin"
println(s.length)      // 6
println(s.uppercase()) // KOTLIN

Practice String operations

Write a fun main(). Declare val name = "kotlin". Print with println(name.length) and println(name.uppercase()).

Sample solution

Main.kt
fun main() {
    val name = "kotlin"
    println(name.length)
    println(name.uppercase())
}

Expected output

6
KOTLIN

String concatenation

You can concatenate two strings with +: "Hello " + "Kotlin" → "Hello Kotlin". But a more readable approach is a string template: "Hello $name".

Templates are shorter than concatenation and convert the variable's type to a string automatically. Numeric values can also be embedded directly as $n.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does `"Kotlin".length` return?

  • 5
  • 7
  • It throws an error
  • 6 Correct answer

Explanation: "Kotlin" has 6 characters.

2 String: match the terms

Term Meaning
length Length
uppercase() Uppercase
substring() Substring
lowercase() Lowercase

3 String: fill in the missing part

Question
val s = "kotlin"
println(s.____())
Answer
val s = "kotlin"
println(s.uppercase())

Explanation: uppercase() converts the String to uppercase.

All lessons in Beginner