On Wed, May 18, 2016 at 10:47 AM, Steven D'Aprano wrote:
> Getting the middle N seems like it ought to be easy:
>
> s[N//2:-N//2]
>
> but that is wrong. It's not even the right length!
>
> py> s = 'aardvark'
> py> s[5//2:-5//2]
> 'rdv'
>
>
> So after spending a ridiculous amount of time on what s
On Thu, 19 May 2016 01:47:33 +1000, Steven D'Aprano wrote:
> Is this the simplest way to get the middle N characters?
This will return a sub-string of any length starting at any
point. This is the way the old VB mid$ function worked.
def mid(string, start, length):
# start begins at 0
i
On Thu, 19 May 2016 02:54 am, Peter Otten wrote:
> Peter Otten wrote:
>
> def mid(s, n):
>> ... shave = len(s) - n
>> ... if shave > 0:
> shave += len(s) % 2
>> ... shave //= 2
>> ... s = s[shave:shave+n]
>> ... return s
>
>> Not exactly the same res
On Thu, 19 May 2016 03:00 am, MRAB wrote:
> I think your results are inconsistent.
>
> For an odd number of characters you have "abc" + "de" + "fg", i.e. more
> on the left, but for an even number of characters you have "a" + "bcd" +
> "ef", i.e. more on the right.
Correct. That's intentional.
On Thu, 19 May 2016 07:28 am, Random832 wrote:
> My take:
>
> def mid(string, n):
> if n > len(string): n = len(string)
> if n <= 0: n = 0
> offset = int(len(string)/2+n/2)
> return string[offset:offset+n]
>
> It doesn't get the same result as yours when the length is odd and N i
On Thu, 19 May 2016 07:35 am, Grant Edwards wrote:
> On 2016-05-18, Steven D'Aprano wrote:
>
>> Getting the middle N seems like it ought to be easy:
>
> I'm still trying to figure out when one would want to do that...
I wanted to centre some text and truncate it to a fixed width. So I needed
t
On 2016-05-18, Steven D'Aprano wrote:
> Getting the middle N seems like it ought to be easy:
I'm still trying to figure out when one would want to do that...
--
Grant Edwards grant.b.edwardsYow! My CODE of ETHICS
at is vacat
On Wed, May 18, 2016, at 11:47, Steven D'Aprano wrote:
> So after spending a ridiculous amount of time on what seemed like it
> ought
> to be a trivial function, and an embarrassingly large number of
> off-by-one
> and off-by-I-don't-even errors, I eventually came up with this:
>
> def mid(string,
On 2016-05-18 16:47, Steven D'Aprano wrote:
Extracting the first N or last N characters of a string is easy with
slicing:
s[:N] # first N
s[-N:] # last N
Getting the middle N seems like it ought to be easy:
s[N//2:-N//2]
but that is wrong. It's not even the right length!
py> s = 'aardvark'
Peter Otten wrote:
def mid(s, n):
> ... shave = len(s) - n
> ... if shave > 0:
shave += len(s) % 2
> ... shave //= 2
> ... s = s[shave:shave+n]
> ... return s
> Not exactly the same results as your implementation though.
The extra line should fix th
Steven D'Aprano wrote:
> Extracting the first N or last N characters of a string is easy with
> slicing:
>
> s[:N] # first N
> s[-N:] # last N
>
> Getting the middle N seems like it ought to be easy:
>
> s[N//2:-N//2]
>
> but that is wrong. It's not even the right length!
>
> py> s = 'aardv
On Wed, May 18, 2016 at 9:47 AM, Steven D'Aprano wrote:
> Extracting the first N or last N characters of a string is easy with
> slicing:
>
> s[:N] # first N
> s[-N:] # last N
>
> Getting the middle N seems like it ought to be easy:
>
> s[N//2:-N//2]
>
> but that is wrong. It's not even the righ
12 matches
Mail list logo