In Python, we have the reverse() method to reverse the sorting order of the elements in List, Tuple, or String in place.
Python Reverse with List
Here we have a list of integers. We need to reverse its items.
my_list = [1, 5, 3, 7, 2]
my_list.reverse()
print(my_list)
Output:
2, 7, 3, 5, 1
Below we have a list with string values. We need to reverse its items.
code = ['React', 'HTML', 'CSS']
code.reverse()
print(code)
Output:
CSS, HTML, React
Python Reverse with String
Below we have a string. We will reverse its spelling using reverse() method:
my_string = "hello"
my_list = list(my_string)
my_list.reverse()
reversed_string = "".join(my_list)
print(reversed_string)
Output:
olleh
Related:
Python Basics
List, Set, Tuple, and Dictionary in Python
Python Reverse List items
Python Round() Function
Python Multiline comment
Power in Python | Python pow()
Python range() Function
Square Root in Python
Python for i in range