String Basics
Create and combine strings.
String Basics is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a String?
A String in Scala is a sequence of characters, like text you read or type.
You write a literal string by wrapping text in double quotes. Strings are one of the most common types you will work with in backend code.
val greeting = "Hello, Scala!"
println(greeting)Declaring String Values
Use val for a string that never changes, and var if you need to reassign it.
Scala infers the type String automatically, but you can also write it explicitly.
val name: String = "Ada"
var mood = "happy"
mood = "excited"
println(name)
println(mood)A Runnable Greeting
Let's put a string inside a complete program. An object extending App runs the code in its body.
This prints a friendly message to the console.
object Main extends App {
val message = "Welcome to Scala strings!"
println(message)
}Strings Are Immutable
A Scala String is immutable: once created, its characters never change.
Methods that seem to modify a string actually return a brand new string, leaving the original untouched.
val s = "abc"
val upper = s.toUpperCase
println(s)
println(upper)String Length
The length method tells you how many characters a string contains.
Spaces and punctuation count too, since they are characters.
val word = "backend"
println(word.length)
val phrase = "a b"
println(phrase.length)Joining Strings with +
You can join, or concatenate, strings using the + operator.
This is handy for building messages from several pieces.
val first = "Func"
val second = "tional"
val result = first + second
println(result)Mixing Strings and Numbers
When you use + between a string and a number, Scala converts the number to text and joins them.
This makes it easy to build readable output.
val count = 3
val line = "You have " + count + " tasks"
println(line)Accessing a Character
Strings are indexed starting at 0. Use parentheses with an index to get a single Char.
The first character is at index 0, the second at index 1, and so on.
val s = "Scala"
println(s(0))
println(s(1))
println(s(4))Comparing Strings
Use == to check if two strings have the same characters. In Scala this compares values, not memory addresses.
Comparison is case sensitive, so "Hi" is not equal to "hi".
println("hello" == "hello")
println("Hi" == "hi")Empty and Blank Strings
An empty string "" has length 0. A string of only spaces is not empty, but it is blank.
Use isEmpty to test for an empty string.
val empty = ""
val spaces = " "
println(empty.isEmpty)
println(spaces.isEmpty)
println(spaces.length)Escape Characters
Some characters need an escape with a backslash. Use \n for a new line, \t for a tab, and \" for a literal quote.
These let you include special symbols inside a normal string.
val text = "Line one\nLine two"
println(text)
val quote = "She said \"hi\""
println(quote)Quick Check
Test your understanding of basic Scala strings.
Recap
You learned that Scala strings are immutable sequences of characters created with double quotes.
You can find their length, join them with +, access characters by index from 0, compare them with ==, and use escapes like \n and \".
Frequently asked questions
Is the “String Basics” lesson free?
Yes — the full text of “String Basics” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.
What will I learn in “String Basics”?
Create and combine strings. You practise Scala for Backend Engineering & Functional Programming 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 Scala for Backend Engineering & Functional Programming?
No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “String Basics” 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 Scala for Backend Engineering & Functional Programming lesson?
Yes. Every Scala for Backend Engineering & Functional Programming 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.