How to Insert a Character into a String at a Specific Index in Python?

As a developer working on various projects across the USA, In one of my projects, I came across a scenario where I needed to insert a character into a string at a specific index in Python, which made to explore more about this topic. After researching I found three effective ways to accomplish this task. In this tutorial, I will explain all the methods with suitable examples.

Insert a Character into a String at a Specific Index in Python

Let us learn all three important methods to insert a character into a string at a specific index in Python.

Read How to Create a String of N Characters in Python?

Method 1: Use String Slicing and Concatenation

One way to add a character at a specified position in a Python string is by using string slicing and concatenation. Here’s an example:

def insert_char(string, char, index):
    return string[:index] + char + string[index:]

# Example usage
name = "John Doe"
modified_name = insert_char(name, "Q", 5)
print(modified_name) 

Output:

John QDoe

In the screenshot below you can see the executed example code with the output

Insert a Character into a String at a Specific Index in Python

The insert_char function inserts a character into a string at a specified index. It does this by splitting the string into two parts: before and after the index. Then, it combines these parts with the character in between.

Check out How to Split a String into Equal Parts in Python?

Method 2: Use List Manipulation

Another approach to inserting a character into a Python string at a certain index is by converting the string to a list, modifying the list, and then converting it back to a string. Here’s an example:

def insert_char(string, char, index):
    string_list = list(string)
    string_list.insert(index, char)
    return ''.join(string_list)

# Example usage
city = "New York"
modified_city = insert_char(city, "X", 4)
print(modified_city) 

Output:

New XYork

In the screenshot below you can see the executed example code with the output

How to Insert a Character into a String at a Specific Index in Python

In this method, we first convert the string to a list using the list() function. Then, we use the insert() method of the list to insert the character at the specified index. Finally, we use the join() method to convert the modified list back into a string.

Read How to Insert a Python Variable into a String?

Method 3: Use f-strings

If you are using Python 3.6 or higher, you can leverage the power of f-strings to add a substring at a specific index. Here’s an example:

def insert_char(string, char, index):
    return f"{string[:index]}{char}{string[index:]}"

# Example usage
state = "California"
modified_state = insert_char(state, "Z", 7)
print(modified_state)

Output:

CaliforZnia

In the screenshot below you can see the executed example code with the output

Insert a Character into a String at a Specific Index in Python f-string

In this method, we use an f-string to create the modified string. Inside the f-string, we use string slicing similar to Method 1, but we directly insert the character {char} within the f-string.

Check out How to Split a String by Index in Python?

Conclusion

In this tutorial, I have helped you learn how to insert a character into a string at a specific index in Python. We have discussed methods like using string slicing and concatenation, list manipulation, and the use of f-string.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.