That is an unexpected return value.
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.
Thanks for reply.
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
If you are enjoying the philosophy of this, I would definitely recommend this video from 3blue1brown: Music And Measure Theory - YouTube 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:
'' 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.