codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

8 / 68 topics
1Introduction to Kotlin2Kotlin Installation3Hello World Program4Variables and Data Types5Operators in Kotlin6Control Flow Statements7Functions in Kotlin8Strings and Templates9Collections in Kotlin10Null Safety
Tutorials/Kotlin/Strings and Templates
🎯Kotlin

Strings and Templates

Updated 2026-04-20
2 min read

Introduction

Strings are fundamental data types used extensively in programming, including Kotlin. In this section, we will explore how strings are handled in Kotlin, focusing on string literals, concatenation, and the powerful feature of string templates.

String Literals

Kotlin supports two main types of string literals: String and Raw Strings.

Simple String Literals

Simple string literals are enclosed in double quotes ("). They allow for basic escape sequences like \n (newline) and \" (double quote).

val greeting = "Hello, Kotlin!"
println(greeting)

Raw String Literals

Raw strings are enclosed by triple double quotes ("""). They can contain multi-line text and do not interpret escape characters unless explicitly escaped with a backslash.

val rawString = """
    This is a raw string.
    It can span multiple lines.
"""
println(rawString)

String Concatenation

In Kotlin, strings can be concatenated using the + operator or by using template expressions.

Using the + Operator

The + operator concatenates two strings and returns a new string.

val firstName = "John"
val lastName = "Doe"
val fullName = firstName + " " + lastName
println(fullName)  // Output: John Doe

Using Template Expressions

Template expressions allow for embedding variables or expressions directly within strings. This is done using the $ symbol.

val name = "Alice"
val age = 30
val message = "My name is $name and I am $age years old."
println(message)  // Output: My name is Alice and I am 30 years old.

For expressions, you can use curly braces {} to include more complex logic.

val a = 5
val b = 10
val sumMessage = "The sum of $a and $b is ${a + b}."
println(sumMessage)  // Output: The sum of 5 and 10 is 15.

String Interpolation

String interpolation in Kotlin allows for embedding variables or expressions within strings, making it easier to create dynamic content.

val name = "Bob"
val greeting = "Hello, $name!"
println(greeting)  // Output: Hello, Bob!

You can also use expressions within the $ symbol by enclosing them in curly braces {}.

val x = 10
val y = 20
val result = "The sum of $x and $y is ${x + y}."
println(result)  // Output: The sum of 10 and 20 is 30.

Raw String Templates

Raw string templates are useful for creating multi-line strings with embedded expressions. They are enclosed by triple double quotes (""") and can include template expressions.

val name = "Charlie"
val age = 45
val bio = """
    Name: $name
    Age: $age
    Status: ${if (age >= 18) "Adult" else "Minor"}
"""
println(bio)

Best Practices

  1. Use Simple Strings for Single-Line Text: For single-line text, simple string literals are more readable and concise.
  2. Use Raw Strings for Multi-Line Text: When dealing with multi-line strings, raw strings help maintain readability by avoiding the need to escape newline characters.
  3. Avoid Overuse of Template Expressions: While template expressions are powerful, overusing them can make your code less readable. Use them judiciously and consider breaking down complex logic into smaller functions or variables.
  4. Consistent Naming Conventions: Use meaningful variable names in templates to improve code readability.

Conclusion

Kotlin's string handling features, including simple and raw string literals, concatenation using the + operator, and powerful string templates, make it a versatile language for text manipulation. By understanding these concepts and best practices, you can write cleaner, more maintainable Kotlin code.

In the next section, we will dive deeper into other fundamental data types in Kotlin, including numbers, booleans, and arrays. Stay tuned!


PreviousFunctions in KotlinNext Collections in Kotlin

Recommended Gear

Functions in KotlinCollections in Kotlin