string.upper & string.lower
Part of the Fundamentals section of Coddy's Lua journey — lesson 45 of 90.
string.upper() and string.lower().
These functions allow you to convert strings to different cases. The string.upper() function converts all letters in a string to uppercase, while string.lower() converts them to lowercase:
local message = "Hello World"
local uppercase = string.upper(message)
local lowercase = string.lower(message)
print(uppercase) -- outputs: HELLO WORLD
print(lowercase) -- outputs: hello worldThese functions are particularly useful for standardizing user input or creating consistent formatting in your programs. For example, you might convert usernames to lowercase for comparison, or convert headings to uppercase for display purposes.
Challenge
EasyCreate a text formatting system that converts a mixed-case company name to both uppercase and lowercase versions for different display purposes. Declare a variable called companyName and assign it the value "TechCorp Solutions". Use string.upper() to create an uppercase version and store it in a variable called upperName. Use string.lower() to create a lowercase version and store it in a variable called lowerName. Print both the uppercase and lowercase versions on separate lines.
Cheat sheet
Use string.upper() to convert strings to uppercase and string.lower() to convert strings to lowercase:
local message = "Hello World"
local uppercase = string.upper(message)
local lowercase = string.lower(message)
print(uppercase) -- outputs: HELLO WORLD
print(lowercase) -- outputs: hello worldThese functions are useful for standardizing user input or creating consistent formatting in programs.
Try it yourself
-- Declare the company name variable
companyName = "TechCorp Solutions"
-- TODO: Write your code here to create uppercase and lowercase versions
-- Print the results
print(upperName)
print(lowerName)This 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