Ways to remove i’th character from string in Python
Here we are going to discuss how to remove characters from a string in a given range of indices or at specific index position.
So we will discuss different different methods for this.
Naive Method
In this method, we have to first run the loop and append the characters .After that build a new string from the existing one .
test_str = "WelcomeBtechGeeks" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using loop new_str = "" for i in range(len(test_str)): if i != 2: new_str = new_str + test_str[i] # Printing string after removal print ("The string after removal of i'th character : " + new_str)
Output:
RESTART: C:/Users/HP/Desktop/article3.py The original string is : WelcomeBtechGeeks The string after removal of i'th character : WecomeBtechGeeks
So in above eoutput you have seen that we have remove character of position three that is ‘l’.
This method is very slow if we compare with other methods.
- Python Program Enter ‘*’ Between two Identical Characters in a String
- Python Program to Remove any Non-ASCII Characters
- Python Program to Remove Punctuations From a String
Using str.replace()
str.replace() can replace the particular index with empty char, and hence solve the issue.
test_str = "WelcomeBtechGeeks" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using replace new_str = test_str.replace('e', '1') # Printing string after removal # removes all occurrences of 'e' print ("The string after removal of i'th character( doesn't work) : " + new_str) # Removing 1st occurrence of e, i.e 2nd pos. # if we wish to remove it. new_str = test_str.replace('e', '', 1) # Printing string after removal # removes first occurrences of e print ("The string after removal of i'th character(works) : " + new_str)
Output:
RESTART: C:/Users/HP/Desktop/article3.py The original string is : WelcomeBtechGeeks The string after removal of i'th character( doesn't work) : W1lcom1Bt1chG11ks The string after removal of i'th character(works) : WlcomeBtechGeeks
So in above output you can see that first we have replace all ‘e’ present in original word.After that we replace only first occurrence of e.This method is also not very useful but sometime we are using this.
Using slice + concatenation
In this method we will use string slicing.Then using string concatenation of both, i’th character can appear to be deleted from the string.
test_str = "WelcomeBtechGeeks" # Printing original string print ("The original string is : " + test_str) #Removing char at pos 3 # using slice + concatenation new_str = test_str[:2] + test_str[3:] # Printing string after removal # removes ele. at 3rd index print ("The string after removal of i'th character : " + new_str)
Output:
RESTART: C:/Users/HP/Desktop/article3.py The original string is : WelcomeBtechGeeks The string after removal of i'th character : WecomeBtechGeeks
Using str.join() and list comprehension
In this method each string is converted in list then each of them is joined to make a string.
test_str = "WelcomeBtechGeeks" # Printing original string print ("The original string is : " + test_str) # Removing char at pos 3 # using join() + list comprehension new_str = ''.join([test_str[i] for i in range(len(test_str)) if i != 2]) # Printing string after removal # removes ele. at 3rd index print ("The string after removal of i'th character : " + new_str)
Output:
RESTART: C:/Users/HP/Desktop/article3.py The original string is : WelcomeBtechGeeks The string after removal of i'th character : WecomeBtechGeeks
Conclusion:
So in this article we have seen different method to remove characters from a string in a given range of indices or at specific index position.Enjoy learning guys.