Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to print on more than one line?

Asked by 9 years ago

I know about [[these strings]] but I saw something with a backslash once. I forgot how it worked but it was something like this.

print("one /n two")

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The backslash escapes the next character in after it in a string. This lets you make special characters, like the newline character, "\n":

print("one\ntwo")
-- one
-- two

You can also make strings with [[ and ]] instead of ". These don't allow escaping, but you can contain a newline in them plainly:

print([[one
two]])
-- one
-- two

Finally, you can escape a literal newline using a backslash:

print("one\
two")
-- one
-- two

That last one is almost never done, though.


In such a simple case where you know ahead of time where you want your newlines, it makes more sense to just break them into two print statements. It will be easier to read and maintain that way.

print("one")
print("two")
Ad

Answer this question