Lesson 6 of 51 beginner

Strings & Numbers

Text Magic and Math Power

Open interactive version (quiz + challenge)

Real-world analogy

Strings are like friendship bracelets made of letter beads -- you can combine them, split them, find specific beads, and rearrange them. Numbers are like a calculator -- you can add, subtract, multiply, divide, and do all sorts of math. Together, they are the most common types of data you will ever work with!

What is it?

Strings hold text and have powerful methods for searching, transforming, and combining text. String interpolation ($variable) lets you embed values inside strings. Numbers include int (whole) and double (decimal) with full arithmetic support. Dart also provides dart:math for advanced math operations.

Real-world relevance

String manipulation is everywhere: displaying user names, formatting prices, parsing API responses, validating email addresses. Number operations handle everything from shopping cart totals to game scores to GPS coordinates. These are the building blocks of every app.

Key points

Code example

import 'dart:math';

void main() {
  // String interpolation
  var name = 'Flutter';
  var version = 3;
  print('Welcome to $name $version!');
  print('Name has ${name.length} letters');

  // String methods
  var email = 'User@Example.COM';
  print(email.toLowerCase());     // user@example.com
  print(email.contains('@'));     // true
  print(email.split('@'));        // [User, Example.COM]

  // Multiline string
  var message = '''
Dear $name,
You are version $version.
Keep being awesome!''';
  print(message);

  // Number arithmetic
  int apples = 10;
  int friends = 3;
  print('Each gets: ${apples ~/ friends} apples');  // 3
  print('Leftover: ${apples % friends}');            // 1

  // Double for precision
  double price = 29.99;
  double tax = 0.08;
  double total = price + (price * tax);
  print('Total: $${total.toStringAsFixed(2)}');     // $32.39

  // Parsing strings to numbers
  var input = '42';
  var number = int.parse(input);
  print('Double it: ${number * 2}');                 // 84

  // Random numbers
  var rng = Random();
  var dice = rng.nextInt(6) + 1;  // 1-6
  print('You rolled a $dice!');
}

Line-by-line walkthrough

  1. 1. Importing the dart:math library for math functions
  2. 2.
  3. 3. The main function starts
  4. 4. A comment for string interpolation section
  5. 5. Creating a String variable 'name' set to 'Flutter'
  6. 6. Creating an int variable 'version' set to 3
  7. 7. Printing a message with both variables embedded using $ syntax
  8. 8. Using ${} with an expression to print the name's length
  9. 9.
  10. 10. A comment for string methods
  11. 11. An email string with mixed case for demonstration
  12. 12. toLowerCase() converts the entire string to lowercase
  13. 13. contains() checks if '@' exists in the string -- returns true
  14. 14. split('@') breaks the string into a list at the @ symbol
  15. 15.
  16. 16. A comment for multiline strings
  17. 17. Triple quotes begin a multiline string
  18. 18. The $name variable is interpolated inside the multiline string
  19. 19. The $version variable is also embedded
  20. 20. The closing triple quotes end the multiline string
  21. 21. Printing the multiline message
  22. 22.
  23. 23. A comment for number arithmetic
  24. 24. Declaring apples as int 10
  25. 25. Declaring friends as int 3
  26. 26. Integer division ~/ gives 3 (10 divided by 3, no decimal)
  27. 27. The % modulo operator gives remainder 1 (10 mod 3)
  28. 28.
  29. 29. A comment for doubles
  30. 30. A price as a double
  31. 31. A tax rate as a double
  32. 32. Calculating total by adding price plus price times tax
  33. 33. Printing the total formatted to 2 decimal places with a dollar sign
  34. 34.
  35. 35. A comment for parsing
  36. 36. A string variable that looks like a number
  37. 37. int.parse() converts the string '42' to the integer 42
  38. 38. Multiplying the parsed number by 2 and printing the result
  39. 39.
  40. 40. A comment for random numbers
  41. 41. Creating a Random number generator
  42. 42. Generating a random integer from 1 to 6 (like a dice roll)
  43. 43. Printing the dice result

Spot the bug

void main() {
  var price = '19.99';
  var quantity = 3;
  var total = price * quantity;
  print('Total: $total');
}
Need a hint?
Look at the types. Can you multiply a String by an int?
Show answer
price is a String ('19.99'), not a double. You cannot multiply a String by an int. Fix: change to var price = 19.99; (without quotes) or use double.parse('19.99') * quantity.

Explain like I'm 5

Strings are like making friendship bracelets with letter beads. You can put them together ('Hello' + 'World'), find a bead ('Where is the @?'), change all beads to big letters (UPPERCASE), or cut the bracelet and rearrange pieces. Numbers are like your calculator in school -- addition, subtraction, multiplication, division. The $ sign in strings is like a magic window -- when Dart sees $name, it peeks at the jar labeled 'name' and puts whatever is inside right into the sentence!

Fun fact

Dart uses UTF-16 encoding for strings, which means it can handle characters from every human language, including Chinese, Arabic, emoji, and ancient Egyptian hieroglyphics. The string 'Hello' takes 10 bytes in memory (2 bytes per character in UTF-16).

Hands-on challenge

Write a program that calculates the area of a circle. Ask for the radius (use a variable), compute area = pi * r * r, and print the result formatted to 2 decimal places using toStringAsFixed(2). Bonus: Also calculate the circumference!

More resources

Open interactive version (quiz + challenge) ← Back to course: Flutter & Dart