Skip to content
String Formatting
step 1/5

Reading — step 1 of 5

Learn

~1 min readStrings

string.format works like C's printf:

print(string.format("%d + %d = %d", 2, 3, 5))
print(string.format("%.2f", math.pi))     -- 3.14
print(string.format("%-10s|%5d", "hi", 42))  -- left-pad string, right-pad number

Common specifiers:

  • %d integer
  • %f float (%.2f for 2 decimals)
  • %s string
  • %x hex
  • %q quoted string (escaped for re-parsing by Lua)

Unlike Python f-strings, Lua's %s will call tostring() on whatever you pass — handy for debugging.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…