1

I know a way to interleave two strings using Python but it works only if their lengths are equal:

u = 'Abcd'
l = 'Wxyz'
res = "".join(i + j for i, j in zip(u, l))
print(res)

This will give me the correct Output:AWbxcydz But if the strings are u = 'Utkarsh' and l = 'Jain', the same method does not give the correct answer. Can someone suggest a way to do so?

khelwood
  • 52,115
  • 13
  • 74
  • 94
Coder_Uj
  • 7
  • 5

1 Answers1

6

Use zip_longest from itertools.

from itertools import zip_longest

u = 'Abcdefgh'
l = 'Wxyz'
res = "".join(i + j for i, j in zip_longest(u, l, fillvalue=''))
print(res)
Andrew Guy
  • 8,390
  • 3
  • 26
  • 38