print(‘Enter a string’)
x=input()
y=””
for i in x:
y=i+y
print(y)
How does the order of y=i+y & y=y+i is different?
pls explain how 1st condition give a reverse string ?
Thank you!
Hi Prafful,
in the example you’re referring to, we want to reverse a string:
word = input(‘Please enter a word:> ‘)
reverse_string = ”
for char in word:
reverse_string = reverse_string + char
print(reverse_string)
So, we iterate over each ‘char’ (or character) in the string and we append it backwards using reverse string. That’s why we need the order char + reverse_string.
If we had reverse_string + char instead, we’d print back the exact same string we input, without reversing it.
In the future, could you please make sure to include a link to the lecture your question is about? It would make it a lot easier to answer the questions, especially since you’re naming your variables differently.
Best,
Eli
ohk thank you! sorry ,will add lecture link from next time!