Combining Strings & Variables
Part of the Fundamentals section of Coddy's Lua journey — lesson 29 of 90.
The concatenation operator .. allows you to join strings and variables together to form complete sentences.
Here's how to combine a string literal with a variable:
playerName = "Alex"
message = "Welcome, " .. playerName .. "!"
print(message) -- Output: Welcome, Alex!You can also concatenate directly within the print() function without storing the result in a variable:
score = 150
print("Player score is: " .. score) -- Output: Player score is: 150Challenge
EasyCreate a character introduction message by combining a player's name with their health points using string concatenation. Create a variable called playerName and assign it the value "Ranger". Create another variable called healthPoints and assign it the value 85. Use the concatenation operator .. to combine these variables with descriptive text and print the result as a complete sentence.
Cheat sheet
The concatenation operator .. joins strings and variables together:
playerName = "Alex"
message = "Welcome, " .. playerName .. "!"
print(message) -- Output: Welcome, Alex!You can concatenate directly within print():
score = 150
print("Player score is: " .. score) -- Output: Player score is: 150Try it yourself
-- Create variables for player name and health points
-- TODO: Write your code here
-- Print the character introduction message
-- TODO: Print the result using string concatenationThis 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