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
  • 55,782
  • 14
  • 81
  • 108
Coder_Uj
  • 17
  • 6

1 Answers1

7

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
  • 9,310
  • 3
  • 28
  • 40