Write a python program to: Read two strings, then put the second string in the middle ofthe first string. And finally Write the new string in a new file.
Question
Write a python program to:
- Read two strings.
- Put the second string in the middle of the first string.
- Write the new string in a new file.
Solution
Sure, here is a simple Python program that accomplishes this:
# Step 1: Read two strings
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
# Step 2: Put the second string in the middle of the first string
middle_index = len(str1) // 2
new_str = str1[:middle_index] + str2 + str1[middle_index:]
# Step 3: Write the new string in a new file
with open("new_file.txt", "w") as f:
f.write(new_str)
This program first reads two strings from the user. It then calculates the middle index of the first string and inserts the second string at this position. Finally, it writes the resulting string to a new file named "new_file.txt".
Similar Questions
Write a python program that traverses through an input string and prints its characters indifferent lines – two characters per line
How do you write data to a file in Python?Question 10Answera.file.read()b.file.append(data)c.file.write(data)d.file.close()
What method is used to read the entire content of a file in Python?Question 7Answera.file.write()b.file.append()c.file.read()d.file.close()
Develop an assembly language program that swaps the values of two strings, which can be of any length.
How do you append data to an existing file in Python?Question 8Answera.open(filename, 'b')b.open(filename, 'w')c.open(filename, 'a')d.open(filename, 'r')
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.