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".
Sample Input 1
abc
pqr
Sample Output 1
apbqcr
Sample Input 2
abc
pqrst
Sample Output 2
apbqcrst
# Interleave String
s1 = input()
s2 = input()
n = min(len(s1), len(s2))
res = ''
for i in range(n):
res += s1[i] + s2[i]
if n < len(s1):
res += s1[n:]
else:
res += s2[n:]
print(res)
Comments
Leave a comment