-1
String1 = "abcd"
String2 = "uvwxyz"

I want it to merge to look like this: aubvcwdxyz

xlm
  • 6,854
  • 14
  • 53
  • 55

3 Answers3

1

You can use itertools.zip_longest:

from itertools import zip_longest

s1 = "abcd"
s2 = "uvwxyz"
s3 = ''.join(a + b for a, b in zip_longest(s1, s2, fillvalue=''))
print(s3)

Output

aubvcwdxyz

Here's a version that works with Python 2. Spot the difference!

from itertools import izip_longest

s1 = "abcd"
s2 = "uvwxyz"
s3 = ''.join(a + b for a, b in izip_longest(s1, s2, fillvalue=''))
print(s3)
Tagc
  • 8,736
  • 7
  • 61
  • 114
  • 2
    Reminder: works only in python 3 (just posted python 2 version) – MaLiN2223 Jan 26 '17 at 23:49
  • @MaLiN2223 I wish we'd just let Python 2 die but I included the Python 2 version all the same. – Tagc Jan 26 '17 at 23:55
  • @Tagc you are not alone however there are too much soft written in 'old' python. You can merge your answer by using : if sys.version_info[0] >= 3 :) – MaLiN2223 Jan 26 '17 at 23:57
1

If you want to use python 2:

a = list("abcd")
b = list("uvwxyz")
q = list(map(None, a, b))
output = ""
for i in q:
    if i[0] is not None:
        output+=i[0]
    if i[1] is not None:
        output+=i[1]
MaLiN2223
  • 1,290
  • 3
  • 19
  • 40
0

What about iteration_utilities.roundrobin:

>>> from iteration_utilities import roundrobin

>>> ''.join(roundrobin(String1, String2))
'aubvcwdxyz'

1 This is from a third-party library I have written: iteration_utilities.

MSeifert
  • 145,886
  • 38
  • 333
  • 352