Answer to Question #176097 in Python for jayanth

Question #176097

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




1
Expert's answer
2021-03-27T15:35:15-0400
# 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)

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