String literals and escapes
Strings are written in quotes, and special characters are written with a backslash.
In this page:
String literals and escapes
Strings can use single quotes, double quotes, or long brackets [[ ... ]] that span several lines and ignore escapes. Common escapes are \n for newline, \t for tab, \\ for a backslash and \" for a quote. Lua 5.3 also supports \x41 hexadecimal and \u{48} Unicode escapes. Strings are immutable, so operations always return new strings.
Note:
Long brackets are perfect for multi-line text and can use levels like [==[ ... ]==] to contain ]].
Example: String literals and escapes
print('single', "double")
print("tab:\there, quote: \"q\"")
print("hex \x41 and unicode \u{48}")
print([[long
string \n stays raw]])
print("a\\b")
-- Output:
-- single double
-- tab: here, quote: "q"
-- hex A and unicode H
-- long
-- string \n stays raw
-- a\b
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Forgetting to escape a quote inside the same kind of quotes
- Expecting escapes to work inside [[ ]]
- Trying to change a character in place
Chapter Summary
- Use ' " or [[ ]]
- \n \t \\ are common escapes
- Long brackets ignore escapes
- Strings are immutable
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: