Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove char at specific index - python

Tags:

python

I have a string that has two "0" (str) in it and I want to remove only the "0" (str) at index 4

I have tried calling .replace but obviously that removes all "0", and I cannot find a function that will remove the char at position 4 for me.

Anyone have a hint for me?

like image 514
tgunn Avatar asked Jan 07 '13 15:01

tgunn


People also ask

How do I remove a character from a specific index?

The most common approach to removing a character from a string at the specific index is using slicing. Here's a simple example that returns the new string with character at given index i removed from the string s .

How do I replace a character at a particular index in Python?

replace() method helps to replace the occurrence of the given old character with the new character or substring. The method contains the parameters like old(a character that you wish to replace), new(a new character you would like to replace with), and count(a number of times you want to replace the character).

How do I remove a specific character in Python?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


1 Answers

Use slicing, rebuilding the string minus the index you want to remove:

newstr = oldstr[:4] + oldstr[5:] 
like image 140
Martijn Pieters Avatar answered Oct 06 '22 05:10

Martijn Pieters