We’ve been using print this whole time, but Lua comes with a lot more functions built in. For example, Lua has an entire collection of functions for using and manipulating strings. The documentation for these functions can be found on Lua’s Official Website. Additional examples for the string and other libraries can be found in the user-maintained wiki

This string function library contains functions like string.upper to convert a string to all upper cases.

string.upper("hello") -- returns HELLO

There is also an entire library of functions about math with useful functions like math.random that returns a random number, or math.min to get the smaller of 2 numbers.

math.min(100, 250) -- returns 100 math.random() -- can return any decimal between 0 and 1 math.random(0, 100) -- can return any number between 0 and 100, including 0 or 100.

There are too many functions to go over in just this one lesson. More often than not, you won’t be expected to memorize every single function that is available to you. It is much more useful to know how to find the function you need and learn how it is used.

Let’s practice this skill by looking at the Lua documentation for built-in functions.



Browse through the Lua library in another tab and see if you can find the string.len function. Once you’ve found it, we’re going to use it to make a function that prints the length of a string.

First, declare a function called printStringLength. It should have one parameter, input which should receive a string argument when the function is called.

printStringLength("Hello There!")

Then, inside the function, use the string.len function to calculate the length of the input string and then print it.

Try calling the function with the input of Hello there!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?