text= "abcde"
text_list = list(text)
text_list.reverse() # reverse 함수를 사용해ㅑ 문져얼 리스트를 거꾸로 뒤집음
print(''.join(text_list))
s = 'abcde'
print(reversed(s)) # <reversed object at 0x102015650>
print(''.join(reversed(s))) # 'edcba'
reverse
는 list 에만 사용 가능합니다.
하지만 reversed
는 reverse 와는 달리 문자열에도 바로 적용이 가능합니다.
reversed(text) 를 통해 문자열을 거꾸로 뒤집은 후 join으로 연결해 바로 출력할 수 있습니다.
text = "abcde"
print(text[::-1]) # "edcba"
l = ['a', 'b', 'c', 'd', 'e']
print(l[::-1]) # ['e', 'd', 'c', 'b', 'a']
t = ('a', 'b', 'c', 'd', 'e')
print(t[::-1]) # ('e', 'd', 'c', 'b', 'a')