Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

Building Output Messages with String Interpolation

This lesson is built with resources from youtube.com and dart.dev.

Hello! In the previous lesson, you declared variables and printed their values one at a time. Now you’ll put those values inside complete messages. String interpolation lets a Dart program produce output such as 2 items in Food without treating the whole sentence as fixed text. Allow about 35–40 minutes to read, watch the short demonstration, and run the examples.


From a variable to a message

Suppose your program stores an expense category and an item count:

String category = 'Food';
int itemCount = 2;

You can print either value directly with print(category) or print(itemCount). To include them in a sentence, put a dollar sign before each variable name inside a string:

print('$itemCount items in $category.');

The output is:

2 items in Food.

Dart replaces $itemCount and $category with their current values when it creates the string. The period is ordinary text; it remains exactly where you put it. Notice that itemCount is still an int. Interpolation gives it a text representation for the message; it does not change the variable’s type.

In the Strings section of Dart’s official Built-in types guide, read the short explanation of quotes and interpolation. Focus on the distinction between an identifier and a longer expression; you can stop before the discussion of joining strings with +.

Built-in types

Read the Strings section of Dart's official Built-in types guide to see the interpolation rules alongside examples of Dart string literals.

In the "Strings" section, start at the sentence about using single or double quotes. Read the explanation and the two code examples immediately following the interpolation paragraph. Stop before the paragraph beginning "You can concatenate strings". Watch for the short form used with a variable and the braced form used with a longer expression.



When to use braces

The short form, $category, works when you want the value of a single named variable. For an expression—something Dart must evaluate to obtain a value—use ${...}:

int itemCount = 2;

print('Current count: $itemCount');
print('Count after adding one: ${itemCount + 1}');
print('Current count is still: $itemCount');

This prints:

Current count: 2
Count after adding one: 3
Current count is still: 2

Dart evaluates itemCount + 1 inside the braces and inserts the result. It has not assigned a new value to itemCount, so the last line still displays 2. The next lesson will examine arithmetic operators in detail; for now, the important distinction is $variable for a single variable, ${expression} for a calculation or other expression. Braces also work around a single variable, but they are unnecessary in '$itemCount items'.

Watch the interpolation portion of Dart Basics – Variables, literals, concatenation and String interpolation by Johan Jurrius. The instructor first replaces a longer way of assembling a message with $variable, then uses braces for a calculation. You do not need to work through the average calculation shown in the video.

Dart Basics - Variables, literals, concatenation and String interpolation

Watch Johan Jurrius's demonstration of string interpolation to see both forms used in a running Dart program.

Watch the interpolation demonstration. At the start, focus on how dollar-prefixed variables fit inside one quoted message. Near the end, look for the braces around a calculation and compare that syntax with the simpler variable form.

There is one more reason to use braces: they can mark where a variable name ends. If category contains 'Food', then '${category}Budget' produces FoodBudget. Writing '$categoryBudget' instead asks Dart for a variable named categoryBudget.



Run a complete example

Create messages.dart and put this program in it:

void main() {
  String category = 'Food';
  var itemCount = 2;
  double estimatedCost = 12.5;
  final String label = 'Planned expense';

  print('$label: $itemCount items in $category.');
  print('Estimated cost: $estimatedCost');
  print('With one more item: ${itemCount + 1} items.');
  print('Original count: $itemCount');
}

Run dart run messages.dart from the file’s folder. You should see:

Planned expense: 2 items in Food.
Estimated cost: 12.5
With one more item: 3 items.
Original count: 2

The first line combines three values with fixed words and punctuation. The second displays a double without requiring you to convert it to a string yourself. The third evaluates an expression, while the fourth confirms that evaluating it did not update the count.

For a quick way to observe when interpolation uses a variable’s value, add these lines just before the closing } and run the file again:

category = 'Transport';
print('Updated category: $category');

The earlier message still says Food, because it was already printed. The new message uses the current value, Transport.

Both single and double quotes work for interpolated strings. Double quotes can be convenient when the message contains an apostrophe:

print("Today's category is $category.");

If you need to print a literal dollar sign, put a backslash before that sign. In the following line, the first dollar sign is literal and the second starts interpolation:

print('Estimated cost: \$$estimatedCost');

With the example’s value, that prints Estimated cost: $12.5.



Key takeaways

String interpolation builds a message by placing values inside quoted text. Use $variable for a named variable and ${expression} when Dart needs to evaluate something inside the message. The resulting text reflects values when the string is created; interpolation does not change those values.

You can now make program output more informative than a series of separate numbers and words. Next, you’ll work with Dart’s arithmetic operators so those messages can include calculated results.