Loops and Strings in python -


given strings s1 , s2, not of same length. create new string consisting of alternating characters of s1 , s2 (that is, first character of s1 followed first character of s2, followed second character of s1, followed second character of s2, , on.

once end of either string reached, remainder of longer string added end of new string. example, if s1 contained "abc" , s2 contained "uvwxyz", new string should contain "aubvcwxyz". associate new string variable s3.

my attempt is:

s3 = ''  = 0  while < len(s1) , < len(s2):      s3 += s1[i] + s2[i]      += 1      if len(s1) > len(s2):          s3 += s1[i:]      elif len(s2) > len(s1):          s3 += s2[i:] 

s1 = "abcdefg" s2 = "hijk" s3 = "" minlen = min(len(s1), len(s2))     x in range(minlen):     out += s1[x]     out += s2[x] out += s1[minlen:] print out 

a couple of things keep in mind. first, can treat python string array, , can access item @ given index using brackets. also, second last line makes use of splicing, more information, see how can splice string?


Comments

Popular posts from this blog

how to do line continuation in perl debugger for entering raw multi-line text (EOT)? -

javascript - Create websocket without connecting -

android - Linear layout children not scrolling -