2

Given the strings s1 and s2, not necessarily of the same length. Create a new string consisting of alternating characters of s1 and s2 (that is, the first character of s1 followed by the first character of s2, followed by the second character of s1, followed by the second character of s2, and so on.

Once the end of either string is reached, the remainder of the longer string is added to the end of the new string. For example, if s1 contained "abc" and s2 contained "uvwxyz", then the new string should contain "aubvcwxyz". Associate the new string with the variable s3.

My attempt is:

s3 = '' 
i = 0 
while i < len(s1) and i < len(s2): 
    s3 += s1[i] + s2[i] 
    i += 1 
    if len(s1) > len(s2): 
        s3 += s1[i:] 
    elif len(s2) > len(s1): 
        s3 += s2[i:]
7
  • 2
    You should give it a try yourself and let us see what you did and where you got stucked to help you out ! Commented Jul 11, 2015 at 3:31
  • s3 = '' for i in range(0, min(len(s1), len(s2))): s3 = s3 + s1[i] + s2[i] Commented Jul 11, 2015 at 3:32
  • 2
    Better post it in your Question...Never be shame of what you tried...:) Commented Jul 11, 2015 at 3:32
  • s3 = '' i = 0 while i < len(s1) and i < len(s2): s3 += s1[i] + s2[i] i += 1 if len(s1) > len(s2): s3 += s1[i:] elif len(s2) > len(s1): s3 += s2[i:] what about this ? Commented Jul 11, 2015 at 3:40
  • You should edit your question to include the info you're currently giving us Commented Jul 11, 2015 at 3:47

6 Answers 6

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

A couple of things to keep in mind. First, you can treat a python string like an array, and you can access the item at given index using brackets. Also, the second to last line makes use of splicing, for more information, see How can I splice a string?

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it using izip_longest method from itertools module, this way:

import itertools as it
s3 = ''.join(''.join(item) for item in it.izip_longest(s1,s2,fillvalue=''))

DEMONSTRATION:

>>> s1 = 'ABCDEF'
>>> s2 = '123456789'
>>> s3 = ''.join(''.join(item) for item in it.izip_longest(s1,s2,fillvalue=''))
>>> 
>>> s3
'A1B2C3D4E5F6789'

EDIT: in avoiding multiple join:

s3 = ''.join(c for item in it.izip_longest(s1,s2,fillvalue='') for c in item)

4 Comments

I would suggest the following to avoid the multiple joins (which are probably less efficient):''.join(c for item in it.izip_longest(s1,s2,fillvalue='') for c in item)
@tiho...thanks for the comment, will update the answer..:)
Although izip_longest was my first thought, i'm going to have to go with @altblahblah for "grader-friendliness" and "student comprehension". Thanks for trying to teach the OP protocol round here.
@msw, that's right, the OP seems to learn Python and altblahblah answer is more comprehensive, just like you said .. :)
0
a = 'abc'
b= 'uxyz'
c=''
t=0

while t<len(a):
    c+=a[t]+b[t]
    t+=1

c+=b[t:]
print(c)

This should be the bare minimum for you to tweak some more so you learn from this. I suggest adding some conditionals and turning it into a function so it can take so s1 and s2 can be any length and it will still work. That'll be fun~

Comments

0

In python3.x, the following code block works, because Python slices return all of elements in range or an empty string if the index values aren't valid for that string:

n = min(len(s1),len(s2))
s3=""

for i in range(0,n):
    s3 = s3 + s1[i] + s2[i]

s3 = s3 + s1[n:] + s2[n:]

Comments

0

we can first decide which string is longer then write a for loop for that condition.

s3 = ""
i=0
if len(s1)>len(s2):
    for i in range(len(s2)) :
        s3 = s3 + s1[i] + s2[i]
        i=i+1
    s3=s3+s1[i:]
else:
    for i in range(len(s1)) :
        s3 = s3 + s1[i] + s2[i]
        i=i+1
    s3=s3+s2[i:]

Comments

-1
i = 0
s3 = ''
minLen = min(len(s1),len(s2))
while i < minLen:
    s3 = s3+ s1[i] + s2[i]
    i += 1
if maxLen != minLen:
    s3 += s1[minLen:]+s2[minLen:]

2 Comments

How maxLen is used?
Welcome to Stack Overflow. Please improve your answer by adding at least a few words to help explain it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.