Signup/Sign In

Python Program to remove ith character from string

In this tutorial, we will learn many different ways to remove the i'th character from a string in Python. Python. Strings in Python are a sequence of characters. Strings in Python are immutable meaning their values cannot be changed so we will create a new string without the i'th character. For example,

Input: "Python" i=2

Output: "Pthon"

Input: "Coding" i=5

Output: "Codig"

To solve this problem there are a number of different approaches, some of them are-

  1. iterative method
  2. using replace() function
  3. slicing and concatenation
  4. list comprehension and join()

We will be looking at each approach in detail.

Approach 1: iterative method

It is the direct approach of iterating over the string to access each character and appending all the characters of the string into a new string except the i'th character. The program should skip the character present at i-1 index. It is a simple but slow method.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Initialise value of i

Step 3- Initialise a new empty string

Step 4- Run loop till the end of the string

Step 5- Add all the characters except the i'th character in the new string

Step 6- Print the new string

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach. Here, we have used a for loop to access the characters in the string and only added the characters which were not at i'th position in a new string.

# Remove i'th character
string="Python"
i=4
print("original: ",string)
# Initialise new string
new_s= ""
# Removing character
for k in range(len(string)):
    if k!=i-1:
        new_s=new_s+string[k]

# After removal of character
print("After removing: ",new_s)


original: Python
After removing: Pyton

Approach 2: replace() Method

In this approach, we will replace the character at the i'th position with an empty character given as ' '. Since a string is immutable, we will store the value after replacing the i'th character in another string.

The drawback of this program is that it will not give the correct output for duplicate characters in the string. This method can only be used when all the characters of a string are unique.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Initialise value of i

Step 3- Initialise a new empty string

Step 4- Run loop till the end of the string

Step 5- Add all the characters except the i'th character in the new string

Step 6- Print the new string

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach. Here, we have used the replace() function to replace the character present at the (i-1) index with an empty character.

string="Programmers"
i=3
print("original: ",string)
# initialise new string and replace
new_s= string.replace(string[i-1],"")
# after removal of character
print("After removing: ",new_s) 


original: Programmers
After removing: Prgrammers

Approach 3: slicing and concatenation

This approach is the best for solving the problem in an optimized way. We will use the slicing concept to slice the string before and after the ith position. Then we will join both these parts of strings and display the final result.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Initialise value of i

Step 3- Initialise a new string

Step 4- Use slicing to get characters before and after the i'th position

Step 5- Join all the characters returned by slicing using ( + )

Step 6- Print the new string

Python Program 3

Look at the program to understand the implementation of the above-mentioned approach.

string="Studytonight"
i=6
print("original: ",string)
new_s= string[:i-1]+string[i:]

# after removal of character
print("After removing: ",new_s) 


original: Studytonight
After removing: Studyonight

Approach 4: list comprehension and join()

In this method, each element of the string is first converted as an element of a list, and then each of them is joined to form a string except the character at the specified index.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a string with values

Step 2- Initialise value of i

Step 3- Initialise a new string

Step 4- Use list comprehension and join() to add all the required characters and leave the character in the ith position

Step 5- Print the new string

Python Program 4

Look at the program to understand the implementation of the above-mentioned approach. We will join all the characters of the string except the character present at the ith position.

string="Studytonight"
i=7
print("original: ",string)
new_s= ''.join([string[k] for k in range(len(string)) if k!=i-1])
# after removal of character
print("After removing: ",new_s) 


original: Studytonight
After removing: Studytnight

Conclusion

We have discussed four different approaches for removing the i'th character of a string in Python. We have discussed the iterative method, the replace() method, the slicing and concatenation method, and the list comprehension and join() method. Each approach has been explained in detail including the algorithm and the program.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.