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: 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.