Metatables are the underlying implementation of objects in Lua, they define custom behaviors such as addition between 2 objects, subtraction, accessing the value, etc.
Each of those are called metamethods, and they all begin with __.
By default whenever you have a string, it has a metatable attached to it as you can see here:
When attempting to index the string, it instead returns the string library table. Which is why if you run this code, you'll see the __index of the metatable print this out:
You can directly access these by indexing the string directly, instead of going through the string library:
The second print uses the : syntactic sugar, both are equivalent, but the second is preferred for shorter code. The left hand of the : will be inserted as the first argument to string.len, and will result in the equivalent code: string.len(s).
You can create your own tables and define metatables for them to do as you'd like, like so:
Here we define a custom metamethod for the tostring function for the table, which instead calls table.concat to concatenate all table values in sequence with a separator of , inbetween each value.