Unable to understand python reversed function

Viewed 40

Why list is used while printing? Without using list why can't we just print the reverse string?

My code:

name = "prajwal"
name1 = reversed(name)
print(list(name1))

#result:
['l', 'a', 'w', 'j', 'a', 'r', 'p']
1 Answers

The reversed function returns an iterator, not a string. Iterators are special Python objects made to iterate over something (that may be more complicated than a list or a string). You can learn more here.

If you want to reverse a string you can do:

name1 = name[::-1]
Related