Lua .len()

ivaaane's avatar
Published Jul 5, 2024Updated Aug 24, 2025
Contribute to Docs

The .len() method returns the length of a string, measured in bytes. It is equivalent to using the length operator (#).

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours

Syntax

string.len(s)
  • s: The input string whose length is to be determined.

Note: Calling .len() without any arguments will result in an error.

Example

The following example demonstrates the usage of the .len() method:

hello = "hello"
hello_len = string.len(hello)
print(hello_len)

The above code produces the following output:

5

All contributors

Contribute to Docs

Learn Lua on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours