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"
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.