Now that you have learned how to use variables to store and calculate numbers in Lua, it’s time to make your programs more expressive and user-friendly. In this lesson, you will discover how to combine text and variables to create clear, informative messages — an essential skill for any programmer.
You will learn two important ways to work with text in Lua:
- String Concatenation: This means joining pieces of text (strings) together, including variables, to form complete sentences.
- String Formatting: This is a more advanced way to insert variables into text, especially useful for numbers and special formatting.
For example, imagine you are planning a trip and want to share the details:
In the code above, you see how to combine text and variables using .. for concatenation and how to use string.format to neatly display a number with two decimal places. The %.2f tells Lua to format the number with two decimal places.
Expected output:
Being able to communicate information clearly is a big part of programming. Whether you are showing results to a user, logging data, or creating reports, you will often need to mix text and variables. Mastering string concatenation and formatting will help you make your programs more readable and professional.
Let’s get started and see how you can make your Lua programs come alive with dynamic, well-formatted messages!
