Python Program to Replace a Character at a Specific Index


In Python, we can easily replace a character at a specific index by converting the string into a list of characters using the list() method. Then, we modify the character at the desired index and convert the list back to the string using the join() method. We can also replace a character at a specific index using the slicing and replace method. In this article, we will see examples of replacing a character at a specific index in Python using list and join method, the slicing method and replace method.

Method 1: Using list() and join() method

Syntax

The list() method

list(sequence)

The list() method takes a sequence (e.g. string, tuple, or set) as an argument and returns a list with the same elements as the sequence.

The join() method

separator.join(iterable)

The join() method is a string method that concatenates the elements of an iterable (e.g. list, tuple, or set) into a single string, using the specified separator string.

Example 1

Suppose we have a string "Hello, World!" and we want to replace the character 'o' at index 4 with 'a'. We first convert the string to a list using the list() method. This creates a list of characters for the string “Hello, World!”. We can now access the specific character at the specified index and replace it with a new character. After replacing the character we can use the join() method to rejoin the characters in the list to form the new string.

string = "Hello, World!"
index = 4
new_char = 'a'

string_list = list(string)
string_list[index] = new_char
new_string = "".join(string_list)

print(new_string)

Output

Hella, World!

Example 2

In the below example, we replace a character at index 0 with a new character z. we define the string "hello world" and the index of the character we want to replace is 0. We also define the new character 'z'.We convert the string to a list using the list() method. Then we access the character at the specified index and replace it with the new character 'z'.

string = "hello world"
index = 0
new_char = 'z'

string_list = list(string)
string_list[index] = new_char
new_string = "".join(string_list)

print(new_string)

Output

Zello world

Method 2: Using Slicing Method

We use slicing to split the string into three parts: the characters before the index, the new character, and the characters after the index. Then we concatenate these parts using the + operator.

Syntax

string[start:end:step]

Here, the slicing method is a string method that returns a substring of the original string by specifying the starting index (inclusive), the ending index (exclusive), and the step size (optional).

Example

In the below code, we define the string "Hello, World!", the index of the character we want to replace is 4, and the new character is 'a'. Then we use slicing to split the string into three parts: the characters before the index (string[: index]), the new character (new_char), and the characters after the index (string[index+1:]). Finally, we concatenate these parts using the + operator to create the new string.

string = "Hello, World!"
index = 4
new_char = 'a'

new_string = string[:index] + new_char + string[index+1:]

print(new_string)

Output

Hella, World!

Method 3: Using the replace() Method

In this method, we use the replace() method to replace the character at the specified index with the new character.

Syntax

string.replace(old_value, new_value, count)

Here, the replace() method is a string method that returns a copy of the original string with all occurrences of the old_value replaced by the new_value. The count argument is optional and specifies the maximum number of occurrences to replace.

Example

In the below code, we define the string "Hello, World!", the index of the character we want to replace is 4, and the new character is 'a'. We use slicing to split the string into two parts: the characters before the index (string[: index]) and the characters after the index (string[index:]).

Then we use the replace() method on the second part of the string (string[index:]) to replace the first occurrence of the character at the specified index with the new character (new_char). We specify the number 1 as the third argument to replace() to only replace the first occurrence.

Finally, we concatenate the two parts of the string using the + operator to create a new string.

string = "Hello, World!"
index = 4
new_char = 'a'

new_string = string[:index] + string[index:].replace(string[index], new_char, 1)

print(new_string)

Output

Hella, World!

Conclusion

In this article, we discussed how we can replace a character at a specific index with another character. We can simply do it by converting the string to a list of characters and then replacing the character by accessing it by its index and then rejoining the characters of the list by using the join() method. The slicing method slices the string into three parts and after replacing the character we concatenate the parts using the + operator. The replace() method can also be used to replace a character at a specific index.

Updated on: 11-Jul-2023

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements