printf in lua


Lua doesn’t have a C-like printf function. However, it has a Java-like string.format function and it allows variable-length arguments, so rolling you own printf is very easy indeed:

function printf(...)
.. io.write(string.format(...))
end

An alternative is to put f = string.format at the beginning of the program and printf-like behaviour can be achieved as follows:

print(f("The answer is: %d", 42))