Given 2 strings, s1 and s2, create a new string by appending s2 in the middle of s1

In this article, we will write a Python program to create a new string by appending s2 in the middle of s1.

Python program to create a new string by appending s2 in the middle of s1

Given 2 strings, s1 and s2, create a new string by appending s2 in the middle of s1:

# slicing
def append(strr1,strr2):
    n = int(len(strr1) / 2)
    res = strr1[: n] + strr2 + strr1[n:]
    return res


print(append("sourceexample","code"))
print(append("python is   programming","best"))

Output:

sourcecodeexample
python is  best programming