Write a Python function named interleaveChars that is given two lines read from a text file, and returns a single string containing the characters of each string interleaved.

Write a Python function named interleaveChars that is given two lines read from a text file, and returns a single string containing the characters of each string interleaved.


def interleaveChars(name1, name2):

    ''' Returns a single string containing the characters of name1 and name2
        interleaved '''
       
    string = ''

    if len(name1) > len(name2):
        shortest = name2
        longest = name1
    else:
        shortest = name1
        longest = name2

    for i in range(len(shortest)):
        string = string + name1[i] + name2[i]
    string = string + longest[len(shortest):]
   
    return string

# main

input_file = open('P5_text.txt', 'r')

name1 = input_file.readline()[:-1]

while name1 != '':
    name2 = input_file.readline()[:-1]
    print(interleaveChars(name1, name2))
    name1 = input_file.readline()[:-1]


Learn More :