Insert characters at the start and end of a string in Python

avatar

Borislav Hadzhiev

Last updated: Sep 21, 2022

banner

Photo from Unsplash

Insert characters at the start and end of a string in Python #

Use the addition operator to insert characters at the start and end of a string, e.g. new_str = 'before' + my_str + 'after'. The addition (+) operator will return a new string by concatenating the specified strings.

main.py
my_str = 'hadz' new_str = 'bobby' + my_str + '.com' print(new_str) # 👉️ bobbyhadz.com

The example uses the addition (+) operator to insert characters at the start and end of a string.

The values on the left and right-hand sides of the addition (+) operator need to be of compatible types.

Convert any non-string values to a string by using the str() class.

main.py
my_str = 'abc' new_str = str(123) + my_str + str(456) print(new_str) # 👉️ 123abc456

When used with strings, the addition (+) operator concatenates the strings and returns a new string.

Note that we can't mutate a string in place. Strings are immutable in Python.

Alternatively, you can use a formatted string literal.

Insert characters at the start and end of a string using f-string #

Use a formatted string literal to insert characters at the start and end of a string, e.g. new_str = f'before{my_str}after'. Formatted string literals let us include expressions and variables inside of a string by prefixing the string with f.

main.py
my_str = 'hadz' new_str = f'bobby{my_str}.com' print(new_str) # 👉️ bobbyhadz.com
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.
main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # 👉️ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

When using this approach, you don't have to worry about the values being of different types because f-strings automatically convert all values to a string.

main.py
my_int = 123 new_str = f'ab{my_int}cd' print(new_str) # 👉️ ab123cd

If you need to insert a string somewhere in an existing string, use string slicing.

main.py
my_str = 'bobby.com' index = my_str.index('.') print(index) # 👉️ 5 new_str = my_str[:index] + 'hadz' + my_str[index:] print(new_str) # 👉️ bobbyhadz.com

We used the str.index() method to get the index of the period in the string.

The str.index method returns the index of the first occurrence of the provided substring in the string.

The method raises a ValueError if the substring is not found in the string.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

The slice my_str[:index] goes up to, but not including the index of the period.

main.py
my_str = 'bobby.com' index = my_str.index('.') new_str = my_str[:index] + 'hadz' + my_str[index:] print(new_str) # 👉️ bobbyhadz.com

The slice my_str[index:] starts at the period and goes to the end of the string.

You don't have to use the str.index() method if you already know the index of the character before which you need to insert a string.

main.py
my_str = 'bobby.com' new_str = my_str[:5] + 'hadz' + my_str[5:] print(new_str) # 👉️ bobbyhadz.com
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.