Answer to Question #321185 in Python for INTERLEAVE STRING

Question #321185

Interleave String

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 containing a string.

the second line of input will containing a string.

the strings may contain special characters ,digits and string.

output

the output should be the merged string

explanation

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
2022-04-14T11:24:23-0400
first_line = input("Enter the first line: ")
second_line = input("Enter the second line: ")
result_line = ''
k_f = 0
k_s = 0
for i in range(0, len(first_line) + len(second_line)+2, 2):
    if k_f <= len(first_line)-1:
        result_line += first_line[k_f]
    if k_s <= len(second_line)-1:
        result_line += second_line[k_s]
    k_f += 1
    k_s += 1
print(result_line)

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