Vinod,

First time it loops, 
newstring = ‘’
oldstring = ‘Newton’
char = ‘N’
char + newstring = ‘N’ + ‘’ = ‘N’

Second time it loops,
newstring = ‘N’
oldstring = ‘Newton’
char = ‘e’
char + newstring = ‘e’ +’N’ = ‘eN’

Third time it loops,
newstring = ‘eN’
oldstring = ‘Newton’
char = ‘w’
char + newstring = ‘w’ +’eN’ = ‘weN’

Fourth time it loops,
newstring = ‘weN’
oldstring = ‘Newton’
char = ‘t’
char + newstring = ‘t’ +’weN’ = ‘tweN’

Fifth time it loops,
newstring = ‘tweN’
oldstring = ‘Newton’
char = ‘o’
char + newstring = ‘o’ +’tweN’ = ‘otweN’

Sixth/Final time it loops:
newstring = ‘otweN’
oldstring = ‘Newton’
char = ‘n’
char + newstring = ‘n’ +’otweN’ = ‘notweN’

newstring after loop finishes will be ‘notweN’ which is the reverse of ‘Newton’

Let me know if that explanation helps you understand it better. There are built 
in functions to reverse a string that are more efficient than this approach 
that you should be using to reverse a string.

--

Thanks,
Nitin Madhok
Clemson University

CONFIDENTIALITY NOTICE
This e-mail, and any attachments thereto, is intended only for use by the 
addressee(s) named herein and may contain privileged and/or confidential 
information. If you are not the intended recipient of this e-mail, any 
dissemination, distribution or copying of this e-mail, and any attachments 
thereto, is strictly prohibited. If you have received this e-mail in error, 
please immediately notify the sender by e-mail or telephone and permanently 
delete all copies of this e-mail and any attachments.

> On Jan 29, 2018, at 1:42 AM, vinod bhaskaran <bhaskaran.vi...@gmail.com> 
> wrote:
> 
> Hi,
> 
> I am a new beginner in programming.
> I saw a code (given below) to reverse  string.
> 
> newstring = ''
> oldstring = 'Newton'
> for char in oldstring:
>   newstring = char + newstring
> print(newstring)
> 
> 
> 
> Could someone explain how it is traversing to get the string reversed?
> 
> As the new character is adding the char in the new string but how is it
> getting reversed without any line giving the char number to be traversed in
> reverse order.
> 
> Thanks,
> Vinod
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to