Menu
Coddy logo textTech

The tostring() Function

Part of the Fundamentals section of Coddy's Lua journey — lesson 30 of 90.

The tostring() function is Lua's built-in tool for converting any value into its string representation.

Here's how tostring() works with different data types:

score = 150
scoreString = tostring(score)
print(scoreString)  -- Output: "150"

isActive = true
statusString = tostring(isActive)
print(statusString)  -- Output: "true"

This function is particularly useful when you need to ensure a value is a string before concatenation. While Lua often handles type conversion automatically, using tostring() makes your intentions clear and prevents potential issues.

level = 5
message = "Player level: " .. tostring(level)
print(message)  -- Output: "Player level: 5"
challenge icon

Challenge

Easy

Convert different data types to strings and verify the conversion by checking their types. Create a variable called playerScore and assign it the value 1250. Create another variable called gameComplete and assign it the value false. Use the tostring() function to convert both variables to their string representations, storing the results in new variables called scoreString and statusString. Use the type() function to print the data type of both converted variables to confirm they are now strings.

Cheat sheet

The tostring() function converts any value into its string representation:

score = 150
scoreString = tostring(score)
print(scoreString)  -- Output: "150"

isActive = true
statusString = tostring(isActive)
print(statusString)  -- Output: "true"

Use tostring() to ensure values are strings before concatenation:

level = 5
message = "Player level: " .. tostring(level)
print(message)  -- Output: "Player level: 5"

Try it yourself

-- Create variables
local playerScore = 1250
local gameComplete = false

-- TODO: Write your code here
-- Convert the variables to strings using tostring()
-- Store the results in scoreString and statusString
-- Use type() function to print the data types
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals