Why `str.replace` repeat `__new` so many times when `__old` is an empty string?

企äøšå¾®äæ”ęˆŖ图_16825793789174
That is an unexpected return value. :joy:

Question: Where are all of the empty strings in the string "abc"? Itā€™s a bit of a philosophical one, but itā€™s not unreasonable to say that thereā€™s one at the start and end of the string, plus one between every pair of characters. Itā€™s those empty strings that you just replaced with pipes. :slight_smile:

Thanks for reply. :smile:
I agree with the philosophical part of what you said. But I donā€™t think this is intuitive and pythonic. I was confused.

The behavior is pythonic because Python slices exact at those len(s)+1 positions. If one knows that python behavior, it is reasonable to expect the exhibited behavior.

I do find this highly unexpected! And the explanations donā€™t really explain the behavior.

In effect, when Python finds ā€œnothingā€, it replaces this with ā€œ|ā€. Well, I see an infinite number of ā€œnothingsā€ between ā€œaā€ and ā€œbā€, so why does it not do an infinite number or replacements?

Take a set of books in a row. (These are the characters in your string.) Ask someone to insert their hand between two books. How many places can they do that?

Or: Ask someone to point to the gaps between the books. How many are there?

Either way, I think youā€™ll find that most people follow a fairly standard fencepost counting technique: there is precisely one ā€œnothingnessā€ between every adjacent pair of books. Not an infinite number of them, but just that many. Of course, if you then say ā€œInsert this book between any two booksā€, youā€™ve just created two new gaps (either side of the inserted one) where there previously was one; so in that sense, youā€™ll never ā€œrun outā€ of gaps. But you still have only a limited number of gaps.

Nope! May be it is a bad example, but between any two books I can put 1 hand or 2 hands! And I can call friends, and they can put all their hands also next to mine.

It is like "What is the count of numbers that fit between 1 and 2? Is it zero or infinite?

But your hand is not nothing. Thatā€™s exactly the point. There is only one nothing between two adjacent things.
And see how nice this works:

>>> 'abc'.replace('','|')
'|a|b|c|'
>>> '|a|b|c|'.replace('|','')
'abc'

Nothing unexpected here.

Thatā€™s very true, but thatā€™s what happens when you add something in a gap: you get new gaps beside it. You can put your left hand in, then take your left hand out, put your right hand in, take your right hand out, and I swear someoneā€™s going to do the hokey pokey at some point, but otherwise, the place you put the right hand is the same gap that you put the left hand in. You canā€™t find two gaps in there without having something to separate them. (Like I said, this DOES get rather philosophical.) You can put both hands in at once, but thatā€™s like replacing the empty string with a two-character string - itā€™s still just one gap.

Well, there are no integers between 1 and 2, so that deals with the ā€œzeroā€ option; but there is a gap between them, as can be proven by the fact that they are not equal. The same philosophical question can be asked: can you ever ā€œfill upā€ the space between 1 and 2 with numbers? How many gaps are there? Thereā€™s really only one gap, until you put a number into it. Pick any two numbers, and the same will be true - if they are unequal, there is a gap between them. You can measure the size of that gap (aka the distance between the numbers - on the real number line, thatā€™s simply a - b) but you canā€™t count how many gaps there are. All you can say is, no matter how many numbers you jam into that gap, thereā€™s still some gap left :slight_smile:

If you are enjoying the philosophy of this, I would definitely recommend this video from 3blue1brown: https://www.youtube.com/watch?v=cyW5z-M2yzw It goes into a lot of the concepts of ā€œcovering spaceā€ with numbers, and as youā€™ll see, the fact that you can always insert another fraction in a gap doesnā€™t mean there are ā€œmore gapsā€ or anything, just that numbers donā€™t really have size (unless you give them size). Itā€™s fascinating stuff and has a lot of odd real-world applications.

What is your concrete proposal for a behavior that an actual physical computer could accomplish.

Ullix, this is a help forum for sincere questions, not a saloon for verbal jousting. I gave the correct explanation: replace matches '' to slice positions and inserts one replacement at each position, which are also the places where vertical slice cursors can go. Enter 3 letters into a discuss reply box. There are exactly 4 possible slice/cursor positions. Not 10, not infinity, exactly 4. Each position gets 1 replacement. replace follows the standard rule that replacements are not rescanned and do not create new matches. If kai understands this, kai will understand other part of Python and parts of other languages that follow the same rule or even a variation.

1 Like