I need to take an word(string) as an input and print the word backwards | Sololearn: Learn to code for FREE!

+1

I need to take an word(string) as an input and print the word backwards

Eg:input:bad Output:dab This is the code I've written x=str(input("Enter a word :")) print(x) i=len(x)-1 for i in range(len(x)): print(x[i]) i-=1 Is my logic correct because I'm not getting the output

3/30/2020 7:57:57 AM

Karthik Eyan

5 Answers

New Answer

+3

variable 'i' in the for loop overrides the variable 'i' before for loop. You can fix your code like this for _ in range(len(x)): print(x[i], end='') #else you get output in newline i-=1 or for i in range(len(x)-1, -1, -1): print(x[i], end='') There is inbuilt way of doing it like x = x[::-1]

+2

Could you give me an up vote.... just asking

+1

I think this code will reverse the order of words in a sentence rather than printing the word backwards

+1

https://www.w3schools.com/python/python_howto_reverse_string.asp This link helped me

0

Thank you for you're help