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']
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']
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]