Strings

The string data type is a sequence of characters, such as letters, numbers, and symbols. It's the data type for storing most text-based information.

Declaring a String

To declare a string variable, put quotes around the characters. It's more common to use double quotes ("), but single quotes (') also work. The following example declares the string1 variable as the string Hello World!.


1local string1 = "Hello world!"
2local string2 = 'Hello world!'
3print(string1) -- Hello world!
4print(string2) -- Hello world!
5

If you want to include a single or double quote in your string, wrap your string around the other type of quote or use an escaped quote:


1local string1 = 'Hello "World"!'
2print(string1) -- Hello "World"!
3local string2 = "Hello 'World'!"
4print(string2) -- Hello "World"!
5

To include both single and double quotes in a string, or to create multi-line strings, declare them using double brackets:


1local string1 = [[Hello
2World!
3Hello "World"!
4Hello 'World'!]]
5
6print(string1)
7--[[ Resulting output:
8Hello
9World!
10Hello "World"!
11Hello 'World'!
12]]
13

Combining Strings

To combine strings, concatenate them with two dots (..). Concatenating strings doesn't insert a space between them. If you want a space between two strings, include it at the end of the first string or concatenate a space between them.


1local hello = "Hello"
2local helloWithSpace = "Hello "
3local world = "world!"
4
5local string1 = hello .. world
6local string2 = helloWithSpace .. world
7local string3 = hello .. " " .. world
8
9print(string1) -- Hello
10print(string2) -- world!
11print(string3) -- Hello world!
12

The print() function takes multiple arguments and combines them with spaces. To combine strings while printing without needing to add extra spaces to the strings, use commas instead of two dots:


1local hello = "Hello"
2local world = "world"
3local exclamationMark = "!"
4
5print(hello .. world .. exclamationMark) -- Helloworld!
6print(hello, world .. exclamationMark) -- Hello world!
7print(hello, world, exclamationMark) -- Hello world !
8

Converting Strings

To convert a string to a number, use the tonumber() function. If the string doesn't have a number representation, then the tonumber() function returns nil.


1local string1 = "123"
2local string2 = "Hello"
3
4print(tonumber(string1)) -- 123
5print(tonumber(string2)) -- nil
6

To convert a number to a string, use the tostring() function.


1local number1 = 1
2print(type(number1)) -- number
3
4local string1 = tostring(number1)
5print(type(string1)) -- string
6

Escaping Strings

To escape a double- or single-quote string declaration and embed almost any character, put a backslash (\) before the character. For example:

  • To embed a single quote in a single-quote string, use \'.
  • To embed a double quote in a double-quote string, use \".

Certain characters following backslashes produce special characters rather than escaped characters:

  • To embed a new line, use \n.
  • To embed a horizontal tab, use \t.

You can't escape square bracket string declarations.


1local string1 = 'Hello \'world\'!'
2print(string1) -- Hello 'world'!
3local string2 = "Hello \"world\"!"
4print(string2) -- Hello "world"!
5
6local string3 = "Hello\nworld!"
7
8print(string3) --[[ Resulting output:
9Hello
10World!
11]]
12
13local string4 = "Hello\tworld!"
14print(string4) -- Hello world!
15

Math and Strings

If you perform math operations on a string, Luau converts the string to a number. If the string doesn't have a number representation, it throws an error.


1print("55" + 10) -- 65
2print("55" - 10) -- 45
3print("55" * 10) -- 550
4print("55" / 10) -- 5.5
5print("55" % 10) -- 5
6print("Hello" + 10) -- error: attempt to perform arithmetic (add) on string and number
7