String index starts with index 0. The first character is present at index 0, and the last character is at the index string’s length -1
Use built-in function len(s1) to get the string length.
Next, get the middle index number by dividing string length by 2.
Solution:
Get the first character from both strings, concatenate them, and store them in variable x
Get the middle character from both strings, concatenate them, and store them in variable y
Get the last character from both strings, concatenate them, and store them in variable x
In the end, join x, y, and z and save it in the result variable
print the result
def mix_string(s1, s2):
# get first character from both string
first_char = s1[0] + s2[0]
# get middle character from both string
middle_char = s1[int(len(s1) / 2):int(len(s1) / 2) + 1] + s2[int(len(s2) / 2):int(len(s2) / 2) + 1]
# get last character from both string
last_char = s1[len(s1) - 1] + s2[len(s2) - 1]
# add all
res = first_char + middle_char + last_char
print("Mix String is ", res)
s1 = "America"
s2 = "Japan"
mix_string(s1, s2)
Hint:
len(s1)
to get the string length.Solution:
x
y
x
,y
, andz
and save it in the resultvariable
result