print and io.write
print adds spaces and a newline for you, while io.write prints exactly what you give it.
In this page:
print and io.write
print accepts any number of arguments, separates them with tabs, converts each with tostring and ends with a newline. io.write only accepts strings and numbers, adds no separators and no newline, and returns the file so calls can be chained. Use io.write when you need full control of the output.
Note:
Use io.write together with string.format when you need precise output layout.
Example: print and io.write
print("a", "b", 3)
io.write("no newline", " ", 42, "\n")
io.write("chained "):write("calls\n")
print(nil, true, 1.5)
-- Output:
-- a b 3
-- no newline 42
-- chained calls
-- nil true 1.5
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Passing a table or nil to io.write and getting an error
- Expecting io.write to add a newline
- Expecting print to separate arguments with spaces instead of tabs
Chapter Summary
- print separates arguments with tabs
- print always ends with a newline
- io.write adds nothing extra
- io.write only takes strings and numbers
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: