Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart String Interpolation


Share

Dart String Interpolation

String interpolation means putting a value inside a string.

To do this, write a dollar sign $ followed by the variable name, inside the string:

Example

void main() {
  var name = 'Emma';
  print('Hello, $name!');
}
Try it Yourself »

Result:

Hello, Emma!

Dart replaces $name with the value of the variable.

You can use as many variables as you want in one string:

Example

void main() {
  var name = 'Emma';
  var city = 'Oslo';
  var age = 28;
  print('$name lives in $city and is $age years old.');
}
Try it Yourself »

Result:

Emma lives in Oslo and is 28 years old.

Expressions With ${}

An expression is a piece of code that gives a value, like a + b.

To put an expression inside a string, write it inside ${}:

Example

void main() {
  var price = 20;
  var quantity = 3;
  print('Total: ${price * quantity} dollars');
}
Try it Yourself »

Result:

Total: 60 dollars

You also need ${} when you use a property or a method of a variable:

Example

void main() {
  var name = 'Emma';
  print('${name.toUpperCase()} has ${name.length} letters');
}
Try it Yourself »

Result:

EMMA has 4 letters

If you forget the curly brackets, Dart only uses the variable name and prints the rest as normal text:

Example

void main() {
  var name = 'Emma';
  print('Letters: $name.length');
  print('Letters: ${name.length}');
}
Try it Yourself »

Result:

Letters: Emma.length
Letters: 4


Interpolation vs Concatenation

You can only join a string with another string when you use +.

If you try to add a number to a string, the program does not run:

Example

void main() {
  var age = 28;
  print('Age: ' + age);
}
Try it Yourself »

Result:

main.dart:3:19: Error: A value of type 'int' can't be assigned to a variable of type 'String'.
  print('Age: ' + age);
                  ^

With interpolation, Dart turns the value into text for you:

Example

void main() {
  var age = 28;
  var height = 1.68;
  var isStudent = true;
  print('Age: $age');
  print('Height: $height');
  print('Student: $isStudent');
}
Try it Yourself »

Result:

Age: 28
Height: 1.68
Student: true

Tip: Interpolation is the normal way to build strings in Dart.

It is shorter than + and works with any type of value.


Print a Dollar Sign

To print a real dollar sign, put a backslash before it: \$.

You can also use a raw string, which does no interpolation at all:

Example

void main() {
  var price = 15;
  print('Price: \$$price');
  print(r'Price: $price');
}
Try it Yourself »

Result:

Price: $15
Price: $price

Multi-line Strings

Interpolation also works in multi-line strings:

Example

void main() {
  var item = 'Coffee';
  var price = 3.5;
  var count = 2;
  print('''Receipt
Item: $item
Count: $count
Total: ${price * count}''');
}
Try it Yourself »

Result:

Receipt
Item: Coffee
Count: 2
Total: 7.0


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.