Hello I am trying to do the following problem where i replace a variable in a string with a zero as long as it is in a given area.
S is the string, b is the variable we want to replace with zeros ( not the letter b) and x is the index (an integer) These are passed into the function as parameters and are already defined.
I have this so far -
mystring = ‘’
for l in s:
if l == ‘b’:
l = ‘0’
mystring = mystring + l
this obviously replaces all the b’s with zeros but i only want to do it if the b (variable, not letter) occurs before x (an integer already defined)
I’ve tried pretty much everything I can think of but can’t get it to work and I feel like the answer is so simple.
EDIT: and x is not an integer that occurs in the string, rather it’s an index. e.g. if x is 5 then replace b with zero up until the 5th char in the string.