The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Problem in python programming

Problem in python programming

1
Vote
1
Answer

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!  

1 Answer

365 Team
0
Votes

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!

6 months