7

How do I interleave strings in Python?

Given

s1 = 'abc'
s2 = 'xyz'

How do I get axbycz?

4
  • 4
    No one is going to help you if you don't show some effort. Besides that, this question can be solved using Google. I don't like to say it, but in this case it is true: Google is your friend! Jun 21, 2010 at 10:28
  • 1
    Not sure what to call this - I've gone with "interleaving". Jun 21, 2010 at 10:33
  • 3
    I think I'd call it zip -ping ;)
    – badp
    Jun 21, 2010 at 10:33
  • 1
    related: Most pythonic way to interleave two strings
    – jfs
    Mar 13, 2018 at 20:34

3 Answers 3

16

Here is one way to do it

>>> s1 = "abc"
>>> s2 = "xyz"
>>> "".join(i for j in zip(s1, s2) for i in j)
'axbycz'

It also works for more than 2 strings

>>> s3 = "123"
>>> "".join(i for j in zip(s1, s2, s3) for i in j)
'ax1by2cz3'

Here is another way

>>> "".join("".join(i) for i in zip(s1,s2,s3))
'ax1by2cz3'

And another

>>> from itertools import chain
>>> "".join(chain(*zip(s1, s2, s3)))
'ax1by2cz3'

And one without zip

>>> b = bytearray(6)
>>> b[::2] = "abc"
>>> b[1::2] = "xyz"
>>> str(b)
'axbycz'

And an inefficient one

>>> ((s1 + " " + s2) * len(s1))[::len(s1) + 1]
'axbycz'
7
  • 2
    What to choose? Which one is the "preferably only one obvious way to do it"?
    – Vi.
    Jun 30, 2011 at 19:43
  • @gnibbler And this one ? :) print ''.join(sum([ [a,b] for [a,b] in zip(s1,s2)],[])) . Upvoted though.
    – eyquem
    Jul 21, 2011 at 17:56
  • 1
    I'd choose "".join("".join(i) for i in zip(s1,s2,s3)) since it's probably the easiest to parse with my eyeballs.
    – lysdexia
    Jul 21, 2011 at 20:16
  • @eyquem, sum(...,[]) has quadradic performance (because it makes a new list each time it adds an element) Jul 21, 2011 at 21:38
  • @gnibbler In the doc: "sum(iterable[, start]) Sums start and the items of an iterable from left to right and returns the total." It doesn't say that a new object is created after each adding of an item rather than an in place adding. But you must be right, because if I do: li=[] print id(li) tot = sum([ [a,b] for [a,b] in zip(s1,s2)],li) print id(tot) , I obtain 18718112 and 18748560
    – eyquem
    Jul 22, 2011 at 1:02
2

What about (if the strings are the same length):

s1='abc'
s2='xyz'
s3=''
for x in range(len(s1)):
   s3 += '%s%s'%(s1[x],s2[x])

I'd also like to note that THIS article is now the #1 Google search result for "python interleave strings," which given the above comments I find ironic :-)

2
  • 1
    Stuff like this always happens on SO ;)
    – BoltClock
    Jul 21, 2011 at 16:26
  • And it still is the #1 google search result for python interleave strings
    – cat
    Jan 2, 2016 at 22:42
0

A mathematical one, for fun

s1="abc"
s2="xyz"

lgth = len(s1)

ss = s1+s2

print ''.join(ss[i//2 + (i%2)*lgth] for i in xrange(2*lgth))

And another one:

s1="abc"
s2="xyz"

lgth = len(s1)

tu = (s1,s2)

print ''.join(tu[i%2][i//2] for i in xrange(2*lgth))
# or
print ''.join((tu[0] if i%2==0 else tu[1])[i//2] for i in xrange(2*lgth))
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.