TExcelTAD
Home › Lua › Lua Output: print vs io.write

Lua Output: print vs io.write

Lua has two everyday ways to write text to the screen: print and io.write. They look similar but behave differently, and knowing the difference saves a lot of fiddling with spacing.

print

print accepts any number of values, converts each one with tostring, separates them with a tab and adds a newline at the end:

lua · try it yourself
print("score", 42, true, nil)
print()
print(3.0, 10 / 4)
score	42	true	nil

3.0	2.5

Calling print() with no arguments prints an empty line.

io.write

io.write writes its arguments exactly as given: no separators and no newline. It accepts only strings and numbers (anything else is an error), so add "\n" yourself:

lua · try it yourself
io.write("Loading")
for i = 1, 3 do
  io.write(".")
end
io.write(" done\n")

io.write("x = ", 10, ", y = ", 2.5, "\n")
Loading... done
x = 10, y = 2.5
print io.write
Separator between arguments tab none
Newline at the end yes no
Accepts any value (nil, tables, booleans…) strings and numbers only
Returns nothing the file (io.stdout), so calls can be chained
Whole floats 3.0 3 (formatted with %.14g)

Building lines with string.format

For aligned or formatted output, combine io.write (or print) with string.format:

lua · try it yourself
local rows = {
  { "Apples", 3, 0.5 },
  { "Bread", 1, 2.25 },
  { "Cheese", 2, 4.8 },
}

for _, row in ipairs(rows) do
  io.write(string.format("%-8s x%d  %6.2f\n", row[1], row[2], row[2] * row[3]))
end
Apples   x3    1.50
Bread    x1    2.25
Cheese   x2    9.60

%-8s pads a string to 8 characters (left-aligned), %d prints an integer and %6.2f a number with two decimals in a 6-character field. The string formatting lesson goes into detail.

Chaining and the io library

io.write returns the output file, so you can chain :write calls. io.stdout is the standard output file object; io.stderr is standard error, useful for error messages that shouldn't mix with normal output:

lua · try it yourself
io.write("one "):write("two "):write("three\n")
io.stdout:write("written to stdout\n")
one two three
written to stdout

Note: Reading input with io.read() works on your own computer (local name = io.read("l") reads a line) but the playground has no keyboard input, so the examples in this course never read from stdin.

Printing tables

print on a table shows only its type and memory address, for example table: 0x55d0c8a2f2a0. The address changes on every run, so it is useless for showing contents. Print the fields yourself, or use table.concat for lists:

lua · try it yourself
local colours = { "red", "green", "blue" }
print(#colours .. " colours: " .. table.concat(colours, ", "))
3 colours: red, green, blue

Test yourself

What does io.write("a", "b") output?

  1. a\tb and a newline
  2. ab with no newline
  3. a b and a newline
  4. An error

Which call raises an error?

  1. print(nil)
  2. print({})
  3. io.write(true)
  4. io.write(42)

What does print() with no arguments do?

  1. Nothing
  2. Prints nil
  3. Prints an empty line
  4. Raises an error