In this article let's try to solve below python program
Take Two Strings s1 And s2 From The User. Write a Program to Create a New String by Appending s1 in The Middle of s2
Take Two Strings s1 And s2 From The User. Write a Program to Create a New String by Appending s1 in The Middle of s2
Given two strings s1 and s2 you have to divide string s2 into Two parts and add s1 in the middle of the s2.
For example let's consider
s1 = “hello”
s2 = “world”
If we try to divide the string S2 into two parts we get first string “wor” and second string “ld”.
So If we try to append S1 in middle of S2 The new string will appear as below
wor + hello + ld -----> worhellold
Let's try to solve this using a Python.
Take two string variables s1 and s2 and ask the user to enter two strings.
s1 = input("Enter First String : ")
s2 = input("Enter Second String : ")
Calculate middle index by taking the length of string s2 dividing it by two
middle_index = len(s2) // 2
Take a new variable you string and append the string S1 in the middle of s2 as shown below. print the new string
new_string = s2[:(middle_index + 1)] + s1 + s2[-middle_index:]
print("New String is : ", new_string)
Final program:
s1 = input("Enter First String : ")
s2 = input("Enter Second String : ")
middle_index = len(s2) // 2
new_string = s2[:(middle_index + 1)] + s1 + s2[-middle_index:]
print("New String is : ", new_string)
Output 1:
Enter First String : hello
Enter Second String : world
New String is : worhellold
Output 2:
Enter First String : number
Enter Second String : string
New String is : strinumbering
Conclusion: Try to solve the program by yourself, provide the different input strings and let me know if you have any comments or suggestions.
0 Comments