0

I have two strings:

a ='hellowww'
b ='world'

Expected Output

c = 'hweolrllodwww'

My code:

for x,y in zip(a,b):
    print(x,y)

Its not working in my case.

Note : Length of two strings ,may not be same.

1
  • 1
    from itertools import zip_longest; c = ''.join(s + t for s, t in zip_longest(a, b, fillvalue='')) or c = ''.join(s for tup in zip_longest(a, b, fillvalue='') for s in tup) Commented Sep 13, 2018 at 16:29

1 Answer 1

4

zip stops when the shortest iterable is traversed. You can use itertool module instead via chain and zip_longest:

from itertools import chain, zip_longest

res = ''.join(chain.from_iterable(zip_longest(a, b, fillvalue='')))

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

Comments

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.