How to Reverse a String in Python?

Reverse output of a string can be achieved using slicing and step size in Python, as shown below:

s = "Hello, World!"
reversed_s = s[::-1]
print(reversed_s)

In this code, [::-1] means taking all characters of the string in reverse order by starting from the end with a step of -1, achieving the reverse output of the string. Running the above code will result in !dlroW ,olleH.

bannerAds