Q:

Append new string in the middle of a given string using python programming

belongs to collection: Python String Exercises

0

Append new string in the middle of a given string

Given two strings, s1 and s2. Write a program to create a new string s3 by appending s2 in the middle of s1.

Given:

s1 = "Ault"
s2 = "Kelly"

Expected Output:

AuKellylt

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Use built-in function len(s1) to get the string length.
  • Next, get the middle index number by dividing string length by 2.

Solution:

  • First, get the middle index number of s1 by dividing s1’s length by 2
  • Use string slicing to get the character from s1 starting from 0 to the middle index number and store it in x
  • concatenate x and s2x = x + s2
  • concatenate x and remeaning character from s1
  • print x
def append_middle(s1, s2):
    print("Original Strings are", s1, s2)

    # middle index number of s1
    mi = int(len(s1) / 2)

    # get character from 0 to the middle index number from s1
    x = s1[:mi:]
    # concatenate s2 to it
    x = x + s2
    # append remaining character from s1
    x = x + s1[mi:]
    print("After appending new string in middle:", x)

append_middle("Ault", "Kelly")

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Create a new string made of the first, middle, and... >>
<< Write a python program to create a new string made...