[Tutor] doubt in a program

vinod bhaskaran bhaskaran.vinod at gmail.com
Mon Jan 29 04:41:02 EST 2018


Thanks a lot Nitin. I misunderstood the "char + newstring".
As a newbie to programming as well as newbie to Python trying to grasp
basics. Sure will need the built in functions present for different things
in Python.
Any suggestion good book for python?

Thanks,
Vinod Bhaskaran

On Mon, Jan 29, 2018, 2:47 PM Nitin Madhok <nmadhok at g.clemson.edu> wrote:

> 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.vinod at 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 at python.org
> > To unsubscribe or change subscription options:
> > https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list