What is string.len in Lua?

In Lua, the string.len() function is used to get the size of a string.

Here is a visual representation of this function:

%0 node_1 Hello, Educative! node_3 string.len() node_1->node_3 node_1635967162535 17 node_3->node_1635967162535
Visual representation of the len() function

Syntax

string.len(str)

Parameters

  • The function needs a string as a mandatory parameter.

Return value

  • The function will return the size of the string as an integer.

Example

In this example, we will return the size of our string.

mystr = "Hello, Educative!"
print(string.len(mystr))

Expected output

17

Free Resources