Python String | Replace Character at Specific Position

In programming languages, sorting, cleaning, and modifying data is an important task. Python supports multiple data structures that store different kinds of data using inbuilt functions. The string data type stores the sequence of characters by passing values inside the single or double quotation marks. Replacing or removing characters from an existing string is normal in any programming language. Replacing characters in a string at specific positions allows the user to modify the program according to his needs.

This article will explain different methods to replace a character at a specific position in a string in Python. The following aspects are discussed in this Python blog post:

So let’s get started!

Method 1: Using String Slicing to Replace Specific Characters

Using the String slicing method, the users can replace, remove and break the string by specifying the start and end index position of the given string. Using this method, different positions of sequences, such as string, list, etc., can be accessed easily. Users can perform certain operations on the sequence’s specific position. In the example given below, the string slicing method is used to replace character at the specific position:

Code:

string_value = 'itslinuxfoss'
index_position = 5
new_value = 'h'

string_value = string_value[:index_position] + new_value + string_value[index_position+1:]
print(string_value)

In the above code:

  • The sequence of string value “itslinuxfoss” is stored in a variable.
  • The index position of a specific character is initialized and stored in a variable named “index_position”. The defining index position value must be smaller than the total character value of a string.
  • The string value uses a statement  [:index_position] to remove the character in a specific location and add the new value character at that place. The character after the new value will also be added by specifying the location, such as “[index_position+1:]”.
  • The final string value will be printed on the screen.

Output:

The new character “h” is added at the specific position in the above output.

Method 2: Using List to Replace Specific Character

In the example below, the string value is first converted into the list data structure and then replaced using the index position. Lets see an example of code given below:

Code:

string_value = 'itslinuxfoss'
index_position = 2
new_value = 'F'

value = list(string_value)
value[index_position] = new_value
string_value = "".join(value)

print(string_value)

In the mentioned code:

  • The value “itslinuxfoss” is stored in a variable.
  • The specific replacement position “2” is stored in a variable named ”index_position”.
  • The new character value “F” is stored in another variable named “new_value”.
  • The string_value is passed inside the parentheses of the “list()” function to convert it into a “list”.
  • The new character value is added at the particular position in the list.
  • The “.join()” function is used to join the “value” with an empty string and store it in a variable named “srirng_value”.

Output:

The new character value “F” is added at the index position “2” in the above output.

Method 3: Replace Multiple Specific Characters

In this method, multiple characters are replaced by the same single character at a specific position using the string slicing techniques. Lets see an example of code given below:

Code:

string_value = 'itslinuxfoss'
index_position = [1, 5, 9]
new_character = 'H'

for index in index_position:
    string_value = string_value[:index] + new_character + string_value[index+1:]

print(string_value)

In the above code:

  • The string value “itslinuxfoss” is initialized in a variable.
  • The multiple index values are initialized as a list and stored in a variable named “index_position”.
  • The new character value “H” is stored in a variable named “new_character”.
  • The “for” loop iterates on the list named “index_position” and replaces the character value on the  specified positions.
  • In the above code, Multiple characters of the string are replaced by the same character value, i.e., “H”.

Output:

The new character value “H” is replaced at index positions 1,5, and 9 of the given string.

Method 4: Replace Different Characters at Specific Positions

In the following method, different characters at different positions are replaced using the “for” loop and string slicing techniques:

Code:

string_value = 'itslinuxfoss'
new_character = {1: 'P', 2: 'y', 3: 't'}


for index, replacement in new_character.items():
string_value = string_value[:index] + new_character[index] + string_value[index+1:]

print(string_value)

In the above code:

  • The string value is initialized to a variable named “string_value”.
  • Multiple character values with different positions are initialized as a dictionary. The “key” value of the dictionary stores the index of the string.
  • The “for” statement iterates on the string value and replaces the string value using the string slicing method.
  • Every initialized index has a different value so all the replacements will replace different characters in the given string.

Output:

The output shows that the new characters “P,y, and t” replace the specific characters in the given string based on the specified positions.

Method 5: Using replace() Function

An inbuilt Python function named “replace()” is used to replace some existing characters of the given string with some new characters. Let’s take a look at an example of code below:

Code:

string_value = 'itslinuxfoss'
string = string_value.replace("x", "S")
print(string)

In the mentioned code the character is replaced at a specific location using the “replace()” function. The “replace()” function takes an existing value and a new character value as arguments and replaces the existing character with the new character.  

Output:

The above output verifies that the value “x” is replaced by “S” in the given string.

Method 6: Using Python Regex

Python’s “regex” module provides a “re.sub()” function that replaces a single character or sequence of characters in the given string. Let’s understand the following example:

Code:

import re
val = 'its linux foss Python Guide'
repl_val = re.sub('Python', 'Ubuntu', val)
print(repl_val)

In the above code:

  • The regex module is imported in the program.
  • The string value is defined in a variable.
  • The “re.sub()” function of the regex module takes the old substring and replaces it with a new substring at a specific position.

Output:

The output shows that the original sequence of characters “Python” is replaced with “Ubuntu”.

That’s all from this Python guide!

Conclusion

In Python, “string slicing”, “re.sub()”, “replace()”, and “List Data Structure” methods replace characters at a specific position of the string. We can replace multiple characters of the string at specific positions with the same or different characters using various inbuilt functions like the string slicing function. This post has provided all the details regarding replacing characters at specific positions of Python strings.