Lua Language Basics

Write a Hello World program and learn the basics of the Lua language.

Displaying Text

A Lua program runs from top to bottom without a main function.

print("Hello World!")

print() displays text and adds a line break. Lua accepts several string literal forms.

print("Hello World!")
print('Hello World!')
print "Hello World!"
print([[Hello World!]])

The long-bracket form preserves its contents without processing escape sequences.

Escape Sequences

Use \n for a line break.

print("Hello\nWorld!")
Sequence Meaning
\n Line break
\a Alert sound
\t Tab
\b Backspace
\' Single quote
\" Double quote
\\ Backslash

Long-bracket strings preserve line breaks and backslashes.

print([[Hello
World!
Hello Lua!
]])

Comments

A single-line comment starts with --.

print("Hello World!") -- Display Hello World!

Use --[[ and ]] for a multiline comment.

--[[
This is
a multiline
comment.
]]

Examples

Concatenate strings with ...

print("There are 10 types of people in this world. " ..
      "Those who understand binary and those who don't. " ..
      "Which one are you?")

Escape quotes or use a long-bracket string when displaying quotation marks.

print("He said, \"How dare you do to me like that!\"")
print([[He said, "How dare you do to me like that!"]])