1

Currently, i have a bit string represented as

current = "011"

and what I'm trying to do is to create a new string based of the bit string above with the 1 at index 1 replaced with 011 which would give me:

new = "00111"

The problem I'm having is that when I use the replace function, it replaced all the 1 in the string including the one at index 2 which is not what I desired.

new = current.replace("1","011")
    = 0011011  #not what I wanted

Would appreciate some help on this.

0

1 Answer 1

1

Limit the number of replace to 1 such as below:

new = current.replace("1","011", 1)
Sign up to request clarification or add additional context in comments.

2 Comments

@ArnavBorborah : It is actually resulting by a replace() however, the logical of the solution was not provided into that duplicate.
I guess you could say that, but I personally feel that this doesn't really add any major value to the duplicate; IMO, this would have been better as a comment to supplement the dupe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.