my_string = "Hello, World!" reversed_string = "".join(reversed(my_string)) print(reversed_string)
"!dlroW ,olleH"
There are several ways to reverse a string in Python. On way as shown above is to use the reversed function and join method.
Another option would be to use string slicing like this:
my_string = "Hello, World!" reversed_string = my_string[::-1] print(reversed_string) # Output: "!dlroW ,olleH"
my_string = "Hello, World!" reversed_string = "" for char in my_string: reversed_string = char + reversed_string print(reversed_string) # Output: "!dlroW ,olleH"
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.