Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 14 Online
OpenStudy (anonymous):

help with optional reading think python exercise 8.2. How do I slice and insert new dna into a string and splice it back together again? code attached: #http://www.greenteapress.com/thinkpython/thinkpython.pdf #chapter 8 for loops #brandon MOOC group 3 #oct. 20 prefixes = 'JKLMNOPQ' suffix = 'ack' news=[] for letter in prefixes: news.append( letter + suffix) print news for each word in news if [:1] == 'O': #finds Oack in the list #code to mutate Oack to Ouack elif [:1] == "Q": #finds Qack in the list #code to insert u before ack

OpenStudy (anonymous):

Like this ? prefixes = 'JKLMNOPQ' suffix = 'ack' news=[] for letter in prefixes: if letter == 'O' or letter == 'Q': letter += 'u' news.append( letter + suffix) print news

OpenStudy (anonymous):

Btw, if you really want to "insert" a char into a String, as strings are immutable, you should probably do like this : word = word[0] + 'u' + word[1:], but then you have to replace the word in the list : for index, word in enumerate(news): if word[0] in ['O', 'Q']: word = word[0] + 'u' + word[1:] news[index] = word print news

OpenStudy (anonymous):

Since you've already put it into a list, I would opt to use the list splice method... for i in range(0, len(news)): letter=news[i][:1] if letter=='O'or letter=='Q': news[i]=news[i][:1]+'u'+news[i][1:] As @FabienToune put it, you will need to replace the word after changing it.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!