Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Variables, Constants and Types  ›  Lesson

Strings, Chars and Booleans

Strings 12 minA String is text in double quotes, a Char is one character in single quotes, string interpolation builds text from values, and a Boolean is true or false with no truthy stand-ins.

Strings, Chars and Booleans

Each run in the logbook needs a line a person can read: "Asha ran 8.5 km". Building that line means mixing text with numbers, and deciding whether a run counts as long means working with true and false.

The rule

Double quotes for text, single for one character; s puts values inside.

val name = "Asha"                  // String
val initial = 'A'                  // Char: exactly one character
val km = 8.5
val line = s"$name ran $km km"     // "Asha ran 8.5 km"
  • A String is text in double quotes. Scala strings are Java strings, and they are immutable: name.toUpperCase returns a new string, "ASHA", and leaves name alone.
  • A Char is one character in single quotes. 'A' and "A" are different types.
  • An interpolated string starts with s. Inside it, $name inserts a value and ${…} inserts the result of any expression: s"${km * 1000} metres" gives "8500.0 metres".
  • The f interpolator formats numbers as well: f"$km%.2f km" gives "8.50 km", with exactly two decimal places.
  • Triple quotes make a string that can run over several lines:
val notice = """Saturday long run
Meet at the boathouse, 7 am"""

Handy string methods: name.length is 4, " Ben ".trim is "Ben", and clubName.contains("Run") is true. The + operator joins strings: "Asha" + " ran".

A Boolean is true or false, and comparisons produce one:

val longRun = km >= 10     // false
val finished = true

Conditions in Scala must be real Booleans. if 1 then … or if name then … is a compile error; unlike Python or JavaScript, there are no "truthy" or "falsy" values.

The shortcut

Read the first character of a literal: " means String, ' means Char, s" means text with values spliced in, f" means text with formatted numbers. When output shows a literal $, look for a missing s.

Building one log lineStep 1 of 4
  1. Collect the values

    name is "Asha" and km is 8.5.

Drills

Answer fast — trust the rule.

quick drill

What is the difference between 'A' and "A"?

quick drill

val name = "Asha" then println("$name ran"). What is printed?

quick drill

What does s"${8.5 * 2} km" produce?

quick drill

With val km = 8.5, what is f"$km%.2f km"?

quick drill

What happens with if 1 then "yes" else "no"?

Remember

  • Double quotes make a String, single quotes a Char; s"…" splices values and f"…" formats numbers.
  • Read the first character of a literal to know its kind; a stray $ in output means a missing s.
  • Trap: expecting truthy values — conditions must be Booleans.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainBuilding a log line

Write an expression that turns name = "Ben" and km = 6.25 into the text Ben ran 6.25 km (easy), where the word in brackets comes from a Boolean check of whether the run was at least 10 km. Explain which interpolator you used and why a Boolean is needed.