Lesson 4 of 15

Strings

Strings

Strings in Lua are enclosed in double or single quotes. Concatenate with ..:

local greeting = "Hello"
local name = "World"
print(greeting .. ", " .. name .. "!")

String Library

Lua provides a string library with useful functions:

print(string.len("hello"))       -- 5
print(string.upper("hello"))     -- HELLO
print(string.lower("HELLO"))     -- hello
print(string.rep("ha", 3))       -- hahaha
print(string.sub("hello", 2, 4)) -- ell
print(string.reverse("hello"))   -- olleh

String Format

string.format works like C's printf:

print(string.format("Name: %s, Age: %d", "Alice", 30))

Your Task

Use string operations to produce the expected output.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.