+ 1

Please find the error in this code .it is not printing the reverse of list

list=[] new=input("enter a string : ") print(new) list.append(new) print(list) print(list[::-1])

1st May 2020, 12:36 PM
Shijali Khare
4 Answers
+ 2
This works print([i[::-1] for i in list]) You can't reverse a "string" like that ,it is a single element in list like 1 ,how can you reverse it?
1st May 2020, 12:45 PM
Abhay
Abhay - avatar
+ 1
shijali khare as you can access a string like an array (ie: get the nth char by string[n-1] -- indexes start at zero as for lists), you cab also use the slice notation directly on a string: inp = input("enter a string: ") print(inp) print(inp[::-1]) (quickly edited: I was forgotting the minus sign ;P)
1st May 2020, 7:07 PM
visph
visph - avatar