09. The print toolkit
Since Chapter 07, print has been your window into a
running program. It does more than show one string. This chapter
collects the tricks you use from here on: several values at once, gluing
pieces into a line, and blank lines.
Several values, separated by tabs
Separate values with commas to pass print more than one.
It prints them on one line with a tab between each:
print("Name", "Level", "HP")
print("Keiko", 7, 95)Output (the gaps are tabs, so the columns roughly align):
Name Level HP
Keiko 7 95
Two things to notice. Values can be different types — strings and
numbers mixed freely. And numbers need no quotes; 7 is a
number, "7" a string.
One value, glued with
..
The comma form always tabs between values. For full control of
spacing, build the string with .., the join operator from
Chapter 07:
print("Level " .. 7 .. " reached!")Output:
Level 7 reached!
With .. there is no automatic tab — you decide what goes
between the pieces, spaces included. A number next to ..
turns into text by itself.
Open exercises/09/01-comma-vs-glue.lua. It prints three
values twice: once with commas, once glued with ... Run it
and compare.
Commas or ..: which to
use?
Both are fine. A rough guide:
- Commas: quickest for a row of values when tab spacing is good enough (debugging, quick tables).
..: for a polished sentence with exact spacing and punctuation, like"Keiko has 95 HP".
local name = "Keiko"
local hp = 95
print(name, "has", hp, "HP") -- Keiko has 95 HP
print(name .. " has " .. hp .. " HP") -- Keiko has 95 HPThe second reads like a sentence; the first has tab gaps.
Blank lines
print() with nothing inside prints an empty line. Use it
to space out output so it reads more easily:
print("Chapter 1")
print()
print("Chapter 2")Output:
Chapter 1
Chapter 2
A number is not its text
One last thing. These two look the same but are different types:
print(7) -- the number seven
print("7") -- a string containing the character 7Both display 7, but one is a number and the other text —
different types. Lua can sometimes bridge the gap, but do not
count on it. You meet types in the next part. For now, just know a
number and its text version differ even when they look identical. It
matters in the input chapter, where what the user types arrives as
text.
Homework
Homework files are in exercises/09/homework/.
Problem 1 — A stat row
Open exercises/09/homework/01-stat-row.lua. Using a
single print with commas, print these on
one line: the string Keiko, the number 7, and
the number 95. Run it and notice the tab gaps.
Problem 2 — A polished line
Open exercises/09/homework/02-polished-line.lua. Print
this exact line:
Keiko has 95 HP at level 7
using .. to glue the pieces, with name,
hp, and level from the variables at the top.
The spacing must match exactly.
Problem 3 — Spaced out
Open exercises/09/homework/03-spaced-out.lua. Print
three lines of text with one blank line between each, using
print() for the blanks. Five calls total.
Challenge — Receipt
Open exercises/09/homework/04-receipt.lua. Three items
with prices sit in variables. Print a receipt: each item on its own line
as name .. ": " .. price, a blank line, then a total line
built with ... You worked the total out earlier.
Stuck or finished? Open the homework solutions page.