1

Say there is a specific string : bacdefaxyza.

Now the character a is repeated 3 times. I now want to be able to delete or replace with "" only the a in the middle but not all. Is there a way to do so? i.e. : I need to be able to delete that a from any place I wish ( index of that character is known ). Is it possible?

I tried using the replace() function but it deletes only the first occurrence, or more, but not at a particular index/place that I need.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
powersource97
  • 373
  • 5
  • 12

2 Answers2

0

Since the index is known:

s = 'bacdefaxyza'
idx = 6
s[:idx]+s[idx+1:]
TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

coming to your example, lets assign the string to a variable say s

Use the python interactive shell to test this out

>>>s = 'bacdefaxyza'

lets assign the result to a new variable called k where we are going to replace second 'a' character with empty character ''

>>>k = s[:6]+s[6:].replace('a','')

>>>print(k)
bacdefxyz
Praneeth
  • 902
  • 2
  • 11
  • 25