Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
from itertools import zip_longest; c = ''.join(s + t for s, t in zip_longest(a, b, fillvalue=''))
c = ''.join(s for tup in zip_longest(a, b, fillvalue='') for s in tup)
zip stops when the shortest iterable is traversed. You can use itertool module instead via chain and zip_longest:
zip
itertool
chain
zip_longest
from itertools import chain, zip_longest res = ''.join(chain.from_iterable(zip_longest(a, b, fillvalue=''))) # 'hweolrllodwww'
Add a comment
Required, but never shown
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.
Explore related questions
See similar questions with these tags.
from itertools import zip_longest; c = ''.join(s + t for s, t in zip_longest(a, b, fillvalue=''))orc = ''.join(s for tup in zip_longest(a, b, fillvalue='') for s in tup)