How to mix the two string in python

How to mix the two string in python

AccionLabs
Input:

str1 = "Hello"

str2 = "world"

Output:

Hweorlllod

Solution:

str1 = "Hello"

str2 = "world"

result = ""

for i in range(max(len(str1), len(str2))):

    if i < len(str1):

        result += str1[i]

    if i < len(str2):

        result += str2[i]

print(result)

Note :

This code is concatenating characters from two strings str1 and str2 and storing the result in a third string variable result.


It uses a for loop to iterate through the maximum length of both strings, and if the index i is within the length of str1, it adds the character at that index to result. The same is done for str2.


Finally, the result string is printed out.

Comments