Back to Blog

Lua for Beginners: String Creation, .., # Length & string.upper/lower | Episode 4

Sandy LaneSandy Lane

Video: Lua for Beginners: String Creation, .., # Length & string.upper/lower | Episode 4 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Lua for Beginners: String Creation, .., # Length & string.upper/lower

In this lesson, you’ll learn how to create and manipulate strings in Lua using different syntax styles and built-in functions. We cover string creation with double quotes, single quotes, and long brackets, then demonstrate concatenation, length retrieval, case conversion, and repetition by building a simple formatted name badge.

Code

-- Creating strings with double quotes, single quotes, and long brackets
local greeting1 = "Hello"
local greeting2 = 'World'
local multiline = [[
This is a
multiline string.
]]

-- Concatenating strings with ..
local fullGreeting = greeting1 .. ", " .. greeting2 .. "!"

-- Getting the length of a string with #
local length = #fullGreeting

-- Changing case with string.upper and string.lower
local shout = string.upper(fullGreeting)
local whisper = string.lower(fullGreeting)

-- Repeating strings with string.rep
local separator = string.rep("-", length)

-- Building a formatted name badge
local name = "CelesteAI"
local title = "Lua Instructor"
local badge = separator .. "\n" ..
              string.upper(name) .. "\n" ..
              string.lower(title) .. "\n" ..
              separator

print(multiline)
print(fullGreeting)
print("Length:", length)
print(shout)
print(whisper)
print(badge)

Key Points

  • Strings in Lua can be created using double quotes (" "), single quotes (' '), or long brackets ([[ ]]) for multiline text.
  • The concatenation operator .. joins multiple strings into one continuous string.
  • The length operator # returns the number of characters in a string.
  • string.upper() and string.lower() convert strings to uppercase or lowercase respectively.
  • string.rep() repeats a string a specified number of times, useful for formatting.