Python Program for removing i-th character from a string
In this tutorial, we will learn to remove the i-th character from a string in Python. Strings in Python are a sequence of characters wrapped in single, double, or triple quotes. For a given string, we have to remove the character present at the i-th index in the string. Indexing in a string starts from 0.
Let us look at the sample input and output of the program.
Input: Python, i=3
Output: Pyhon
Input: Studytonight, i=7
Output: Studytnight
To solve this problem in Python we can follow these approaches
- By splitting the string
- Using replace() method
We will be discussing each approach in detail.
Approach 1: splitting method
In this approach, we will split the string into two halves, one before index i and the other after index i. Then we can join these two strings to get the string that will not have the i-th character.
To split the string we will use the concept of slicing. Slicing specifies how to slice a sequence into smaller objects where the starting and ending index is specified by us.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will accept a string and index i
Step 2- In the function, declare a variable that will store characters before the i-th index
Step 3- Another variable will store the characters after the i-th index
Step 4- Join both substrings and return the joined string
Step 5- print value returned by the function
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
We have sliced the string into two substrings and stored each substring in another variable then we have joined the two substrings using the concatenation operator (+). Since, indexing in a string starts with 0, to remove i-th character, we have to pass i-1 as an index in our function.
def remove_char(s, i):
a = s[ : i]
b = s[i + 1: ]
return a+b
string = "Pythonisgood"
# Remove ith index element
i = 5
print(remove_char(string,i-1))
Pythnisgood
Approach 2: using replace() method
In this method, we will replace the character at the i-th index with an empty substring given as ("") using the replace() method.
The replace() is a built-in function. It returns a copy of the string after replacing all the occurrences of the old substring with the new substring.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will accept the string and index i and will remove the character at i
Step 2- Run a loop from 0 to the length of the string
Step 3- Check if the current iteration is equal to the index i
Step 4- If found replace the character at index i with an empty string
Step 5- Return string after replacing
Step 6- Print value returned by the function
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach. We have used a for loop in our program to traverse the string and get the character at the index i. When the character is found replace it with an empty string.
In the function, since indexing in a string starts with 0, to remove i-th character, we have to pass i-1 as an index.
def remove_char(s, i):
for j in range(len(s)):
if j==i:
s=s.replace(s[i],"",1)
return s
string = "Welcome"
# Remove i-th index element
i = 2
print(remove_char(string,i-1))
Wlcome
Conclusion
We have seen two different approaches for removing the i-th character in a string. The first approach is by splitting the string into two halves to remove the i-th character. The second is by using the replace() method of string class.