[Python] Help with for loop output via /r/learnprogramming


[Python] Help with for loop output

Hi,

I've been trying to build a piece of code to duplicate the characters in a string, and save them to new string (so for 'cat' I would get 'ccaatt'). I've been running the code below (note the (tab) indicates a Tab indent as I'm not sure how to format reddit over mobile):

z= "cat"

a= ""

for c in z:

(Tab)p=z.index(c)

(Tab)a=z+(z [p:p+1]*2)

print(a)

My output is: 'cattt'

Shifting the [p:p+1] to [p-1:p] ouputs: 'cataa'

Any idea what I'm doing wrong?

Submitted July 13, 2017 at 01:29PM by 123zxcfgh
via reddit http://ift.tt/2unAnRq

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s