How do I reverse a string in Python?
To reverse a string in Python, you can use the following method:
def reverse_string(string):
return string[::-1]
You can then use the function like this:
This will return the reversed string "olleh".
You can also reverse a string by using the built-in reversed()
function. Here's an example of how to do that:
def reverse_string(string):
return "".join(reversed(string))
You can then use the function like this:
def reverse_string(string):
return "".join(reversed(string))
reverse_string("hello")
This will also return the reversed string "olleh".