Replace a character of a string by it’s index in Python

In this tutorial, we are going to learn about how to replace a character of a string by its index in Python.

For example, we have a string “python” and we have to replace a character which is at an index 2 with “s” which means “t” will be replaced by “s”. so the final output is “pyson”.

Indexing in Python

  • The positions of a string’s characters are numbered from 0, on the left, to the length of the string minus 1, on the right.

For example, we have a string “Hi there” so its indexing is

 

Hithere
01234567
-8-7-6-5-4-3-2-1

So this is the indexing of string in python

 

Addition of two or more strings:

We can add two strings by using simply arithmetic  + operation.

For example-

Input: “hel”+”lo”

Output: “hello”

 

We have many methods to replace a character of a string by its index in Python but here only we discuss only two of them

Using for loop and conditional statement  to replace a character of a string by it’s index in Python

Let’s start coding

  1. 1st we have a string s=”hello” and we want to replace the character which is at index 2 means 1st “l” in “hello” and replace this character with the “i” and final output we want is “heilo”.
  2. Now we have to create a new empty string to store the characters which we will get in below for loop.
  3. Then we will run a for loop that runs equal to the length of string s len(s) times.
  4. In that, we have an if statement which will execute only if i equals that particular index x and at that index, we will add a character that we want to replace.
  5. Else remaining characters we will add into a new string(newStr) one by one with the help of for  loop

 

s="hello"

x=2  #we want to replace character which is at index 2

r="i" #This is character which we want to place at that index

newStr=""

for i in range(len(s)):

    if i==x:
        newStr=newStr+r

    else:

        newStr=newStr+s[i]  

print(newStr)

Output:

heilo

 

By using arithmetic operation

So in this method, we basically brake our string in two-part.

  • 1st part is all the characters before that index which the user wants to replace the character from that position.
  • 2nd part is all the characters after that index
  • At last, we will add the character(which the user wants to place at that index) in the middle of that parts

let’s start coding:

  1. 1st we have a string s=”hello” and we want to replace the character which is at index 2 means 1st “l” in “hello” and replace this character with the “i” and final output we want is “heilo”.
  2. Now we use simple arithmetic addition operation (+) two add those two-part and character which user want to place.
  3. At last, we will print that new string.

More we understand after seeing the code given below

 

s="hello" 
x=2 #we want to replace character which is at index 2 
r="i" #This is character which we want to place at that index 

newStr= s[:x] + r + s[x+1:]

print(newStr)

Output:

heilo

 

let’s understand the above code

s[:x] – It means it will return all the characters from index zero to (x-1)

s[X+1:]- It means it will return all the characters from the index (x+1) to the end.

so we will fetch all the characters except the character at index x from string s and At index x, we have added r which is the character which user wants to replace

 

Thus, we have learned how to replace a character of a string by its index in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *