reversed(my_sequence)
my_sequence : object supporting sequence protocol.
Returns the reversed iterator
Using a list
Details on list
my_list=['One','Two','Three']
print(my_list) # ['One', 'Two', 'Three']
my_list=list(reversed(my_list))
print(my_list) # ['Three', 'Two', 'One']
Using a tuple
Details on tuple
my_tuple=('One','Two','Three')
print(my_tuple) # ('One', 'Three', 'Two')
my_tuple=tuple(reversed(my_tuple))
print(my_tuple) # ('Three', 'Two', 'One')
Using a range
my_range=range (6)
for i in my_range:
print(i, end=' ') # 0 1 2 3 4 5
my_range2=reversed(my_range)
for i in my_range2:
print(i, end=' ') # 5 4 3 2 1 0
using a string
my_str='plus2net.com'
print(list(reversed(my_str)))
# ['m', 'o', 'c', '.', 't', 'e', 'n', '2', 's', 'u', 'l', 'p']
reversing a string
Read more on join()
str='plus2net'
str_rev=''.join(reversed(str))
print(str_rev)
Output
ten2sulp
← Subscribe to our YouTube Channel here