Replace Character in String by Index Position

Python: Replace Character in String by Index Position | How do you Replace Character at Index in String Python?

In this article, we have discussed how to replace a character in a string by Index Position. For your reference, we have outlined several methods for replacing characters in the string like slicing, replacing characters at multiple indexes, replacing multiple characters at multiple indexes, etc. Check out Sample Programs for Replacing Characters on String by Index Position for a better understanding of how to write code.

String – Definition

Strings are one of the most commonly used types in Python. We can easily make them by enclosing characters in quotes. Python considers single quotes to be the same as double quotes. String creation is as easy as assigning a value to a variable.

Examples:

Input:

string="Hello this is BTechGeeks" index=4 character='w'

Output:

Hellw this is BTechGeeks

Replace Character In String by Index Position in Python

There are several ways to replace characters in a string by index some of them are:

Method #1:Using Slicing to replace the character at index

Split a string in three sections in order to replace an index-position n character: characters before nth character, nth character and nth characters. Then add a new string and use the replacement character instead of using the nth character.

Below is the implementation:

Python Program using Slicing to Replace a Character in String by Index Position

# given string
string = "Hello this is BTechGeeks"
# given index
index = 4
# given character
character = 'w'
# using slicing to replace it
string = string[:index]+character+string[index+1:]
# print the string
print(string)

Output:

Hellw this is BTechGeeks

Method #2:Replace characters at multiple indexes

We have few index positions, and at these Index positions, we want to replace all characters. To achieve this, all index positions in the list will be iterated. And substitute the character on that index by cutting the string for each index

Below is the implementation:

Python Program to Replace characters at multiple indexes

# function which replaces the string
def replaceString(string, index, character):
    # replacing the string
    newString = string[:index]+character+string[index+1:]
    # return the string
    return newString


# given string
string = "Hello this is BTechGeeks"
# given index list
indexlist = [1, 4, 8]
# given character
character = 'w'
# Traversing the indexlist
for index in indexlist:
    string = replaceString(string, index, character)
# print the string
print(string)

Output:

Hwllw thws is BTechGeeks

Method #3: Replace multiple characters at multiple indexes

In the example above, we replace all characters with the same substitute characters in given positions. But we may want to have different replacement characters in certain scenarios.
If we have a dictionary with key-value pairs that contain the index positions and substitute characters. We want to substitute the corresponding replacement character in all characters at those index positions. This will be done through all of the dictionary’s key-value pairs. And replace each key with the character in the value field at that index position.

Below is the implementation:

# function which replaces the string
def replaceString(string, index, character):
    # replacing the string
    newString = string[:index]+character+string[index+1:]
    # return the string
    return newString


# given string
string = "Hello this is BTechGeeks"
# given index and characters
charsdict = {1: 'p', 4: 'q', 8: 's'}
# Traversing the charsdict
for index in charsdict:
    string = replaceString(string, index, charsdict[index])
# print the string
print(string)

Output:

Hpllq thss is BTechGeeks

Related Programs: