Updated May 8, 2023
Definition of Lua String Format
String format is the same as the format in C, format in Lua is used to format a particular variable. This format gives us the value in a specific format. By the use of string format in Lua we can format our string messages, which can be easy to understand. string format works as the same way as c language. To use format string we use a simple ‘%’ percentage symbol to represent and print the variable of a different type. To represent different type in Lua we have different symbol available which can be used to print the values of the variable correctly. In the coming section, we will discuss more the format string in Lua in detail for better understanding and implementation.
Syntax:
As discussed string format is used to format the string, by the use of it we can show the string message in a more user-friendly manner, Let’s take a look at its syntax for better understanding see below;
string.format()
As you can see in the above lines of syntax we are using string.format() method, it is very easy to use and handle. Inside this, we can pass the parameter to create the string message. Let’s take practice syntax to understand it better for beginners see below;
e.g.;
string.format("%s", variable_name)
As you can see in the above line of code you can pass the variable name which will print the value. In the coming section, we will discuss more the internal working of the string.format() method in Lua for beginners.
How to Format String in Lua?
As we already discussed format string is a function in Lua, it is used to format the string that’s why it is called a format string in Lua. To format the string we have ‘%’ symbol which has to be used before the different operator. To represent the different types, we have several symbols available to show the value of the variable. Let’s take a look at the different symbol available in Lua for string format see below;
1) To represent string: To represent any string in the string format function we can use ‘s’ with the percentage symbol. This ‘s’ should be followed by the ‘%’ symbol. We can have multiple variables which can be shown inside the string format. Let’s take a look at its syntax for better understanding see below;
e.g. :
string.format("%s", Your_variable)
As you can see in the above lines of syntax to format the string we are using the ‘%s’ symbol to represent it. Followed by the variable name.
2) To represent float: To represent any float in the string format function we can use ‘f’ with the percentage symbol. This ‘f’ should be followed by the ‘%’ symbol. We can have multiple variables which can be shown inside the string format. Let’s take a look at its syntax for better understanding see below;
e.g. :
string.format("%f", Your_variable)
As you can see in the above lines of syntax to format the string we are using ‘%f’ symbol to represent it. Followed by the variable name.
3) To represent decimal: To represent any decimal in the string format function we can use ‘d’ with the percentage symbol. This ‘d’ should be followed by the ‘%’ symbol. We can have multiple variables which can be shown inside the string format. Let’s take a look at its syntax for better understanding see below;
e.g. :
string.format("%d", Your_variable)
As you can see in the above lines of syntax to format the string we are using ‘%d’ symbol to represent it. Followed by the variable name.
4) To represent hexadecimal: To represent any hexadecimal in string format function we can use ‘x’ with the percentage symbol. This ‘x’ should be followed by the ‘%’ symbol. We can have multiple variables which can be shown inside the string format. Let’s take a look at its syntax for better understanding see below;
e.g. :
string.format("%x", Your_variable)
As you can see in the above lines of syntax to format the string we are using the ‘%x’ symbol to represent it. Followed by the variable name.
5) To represent octal: To represent any octal in string format function we can use ‘o’ with the percentage symbol. This ‘o’ should be followed by the ‘%’ symbol. We can have multiple variables which can be shown inside the string format. Let’s take a look at its syntax for better understanding see below;
e.g. :
string.format("%o", Your_variable)
As you can see in the above lines of syntax to format the string we are using the ‘%f’ symbol to represent it. Followed by the variable name.
6) To represent character: To represent any character in the string format function we can use ‘c’ with the percentage symbol. This ‘c’ should be followed by the ‘%’ symbol. We can have multiple variables which can be shown inside the string format. Let’s take a look at its syntax for better understanding see below;
e.g. :
string.format("%c", Your_variable)
As you can see in the above lines of syntax to format the string we are using ‘%f’ symbol to represent it. Followed by the variable name.
In this way, we can show different variable types in lua in the string format function in Lua language. But we need to keep some points in mind while using it;
1) To represent different data types inside the string we have different symbols defined for them. If we do not use a suitable symbol, then it will give us an error.
2) Before every symbol we have to use the ‘%’ symbol to tell the compiler there may be a value that should be printed.
3) We can have different types inside the same function.
Example
In this example, we are trying to showcase the usage of all the symbols available in Lua for string format. We are trying to show integer, string, character, float, and so on by using the string format in Lua. This is a sample example for beginners to understand its implementation and start with it.
Code:
print("Demo to show string format in lua !!")
a1 = "sample for string"
a2 = 1000
a3 = 10.1456
a4 = 'A'
a5 = "Hello"
print("print result of string format !!! ")
print(string.format("Result from the format function is :: %s", a1))
print(string.format("Result from the format function is :: %d", a2))
print(string.format("Result from the format function is :: %f", a3))
print(string.format("Result from the format function is :: %s", a4))
print(string.format("Result from the format function is :: %s", a5))
Output:
Conclusion
By the use of string format, we can modify our string message which can be more under stable by the user. we can represent our values inside it, they give and format the values to a more accurate value. the string format is very easy use and handles also it is an in-built function in Lua. We do not need to use any external library for this.
Recommended Articles
We hope that this EDUCBA information on “Lua String Format” was beneficial to you. You can view EDUCBA’s recommended articles for more information.