How to reverse a string in Lua

In Lua, there are many functions that are useful to manipulate strings. We can use the string.reverse function if we need to reverse a string.

%0 node_1 Hello, Educative! node_3 string.reverse() node_1->node_3 node_1635717539976 !evitacudE ,olleH node_3->node_1635717539976
A visual representation of string.reverse()

Syntax

string = string.reverse(string)

Parameters

The function only needs a valid string as a mandatory parameter.

Return value

The reverse function will reverse the string given as input.

Code

In this example, we will reverse the string "Hello, Educative!":

ourstring = "Hello, Educative!"
print(string.reverse(ourstring))

Expected output

!evitacudE ,olleH

Free Resources