How to Add a Phrase in the middle of a Python String?

Strings are a fundamental data type in Python, and manipulating them is a common task in many programming scenarios. One specific requirement you may encounter is the need to insert a phrase or substring in the middle of an existing string.

Python provides several methods to accomplish this task efficiently. We'll explore different approaches including string concatenation, f-strings, the str.join() method, and slicing techniques.

Using String Concatenation

One straightforward way to add a phrase in the middle of a string is by using string concatenation with the + operator ?

original_string = "Hello, world!"
phrase_to_insert = "beautiful "

middle_index = len(original_string) // 2
new_string = original_string[:middle_index] + phrase_to_insert + original_string[middle_index:]

print(new_string)
Hello, beautiful world!

In this approach, we split the original string at the desired position using slicing, then concatenate the three parts: the first half, the phrase to insert, and the second half.

While string concatenation works well for small strings, it can become inefficient when dealing with large strings since every concatenation operation creates a new string object.

Using F-String Formatting

F-strings provide a concise and readable way to insert phrases into strings ?

original_string = "Hello, world!"
phrase_to_insert = "beautiful"

middle_index = len(original_string) // 2
new_string = f"{original_string[:middle_index]} {phrase_to_insert} {original_string[middle_index:]}"

print(new_string)
Hello, beautiful world!

This approach uses string interpolation with f-strings to insert the phrase at the desired position, offering clean and readable syntax.

Using the str.join() Method

The str.join() method concatenates a list of strings with a specified delimiter ?

original_string = "Hello, world!"
phrase_to_insert = "beautiful"

middle_index = len(original_string) // 2
new_string = ' '.join([original_string[:middle_index], phrase_to_insert, original_string[middle_index:]])

print(new_string)
Hello, beautiful world!

This method is particularly beneficial when you have multiple substrings to insert, as it allows easy joining with a specific delimiter.

Inserting at a Specific Position

You can insert a phrase at any specific character position, not just the middle ?

original_string = "Python programming"
phrase_to_insert = "is awesome for "
insert_position = 7  # After "Python "

new_string = original_string[:insert_position] + phrase_to_insert + original_string[insert_position:]
print(new_string)
Python is awesome for programming

Comparison of Methods

Method Readability Performance Best For
String Concatenation Good Medium Simple operations
F-strings Excellent Fast Modern Python code
str.join() Good Fast Multiple insertions

Conclusion

For most cases, f-strings provide the best combination of readability and performance. Use str.join() when dealing with multiple insertions, and string concatenation for simple scenarios in older Python versions.

Updated on: 2026-03-27T12:36:43+05:30

637 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements