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
EasyConvert 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 typesThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator