Answer to Question #205589 in Python for sudheer

Question #205589

Interleave Strings

Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a string is longer than the other, append the additional characters onto the end of the merged string.Input


The first line of input will contain a string.

The second line of input will contain a string.

The strings may contain special characters, digits and letters.Output


The output should be the merged stringExplanation


For example, if the given strings are "abc" and "pqrs", then alternating characters from each string are merged as "a" from "abc", "p" from "pqr", "b" from "abc" and so on ..., resulting in the merged string "apbqcr".



1
Expert's answer
2021-06-10T15:24:08-0400
s1 = input()
s2 = input()
l1 = len(s1)
l2 = len(s2)
res = ''
if l1 > l2:
	m = l2
	end = s1[m:]
else:
	m = l1
	end = s2[m:]
for i in range(m):
	res += s1[i] + s2[i]
res += end
print(res)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS