-1

I am writing a program in python to reverse a string using function reversed but not getting the output .

myStr="hello"
rev=reversed(myStr)
print(rev)

I am getting the Output - reversed object at 0x000001823DF7DD68

Rishavv
  • 301
  • 1
  • 6

1 Answers1

1

reversed gives you a iterator object and when you print it, you see the string representation of the object reversed object at 0x000001823DF7DD68,

Instead, you can use list slicing to reverse the string

myStr="hello"
print(myStr[::-1])

Or if you want to use the iterator, join it back to a string

myStr="hello"

rev=reversed(myStr)

#Join list back to string by passing it the reverse iterator
print(''.join(rev))

The output in both cases will be

olleh
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40