Menu
Coddy logo textTech

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: 150
challenge icon

Challenge

Easy

Create 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: 150

Try 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 concatenation
quiz iconTest yourself

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

All lessons in Fundamentals