reverse() and reversed() function in python by R4R Team

- reverse() and reversed() both are python in-buid function which are used to reverse the content of the list.
- reverse() is only applicable on list.
- while reversed() is also work on the string data type.

Syntax-

list.reverse()

reversed(list)


program-

l=[1,2,3]
print("List is",l)
l.reverse()
print("Reverse list is",l)

print("Again reverse by reversed() function",*reversed(l))

s="Mytext"
print("string is",s)
print("String in reverse order",*reversed(s))


output-

List is [1, 2, 3]
Reverse list is [3, 2, 1]
Again reverse by reversed() function 1 2 3
string is Mytext
String in reverse order t x e t y M


-In this program, we take a list and reverse the item in the list by both reverse() and reversed() function.
- and then we reverse the string by using the reversed() function.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!