Re: Extract the middle N chars of a string

2016-05-20 Thread boB Stepp
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Wildman via Python-list
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Steven D'Aprano
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Steven D'Aprano
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.

Re: Extract the middle N chars of a string

2016-05-18 Thread Steven D'Aprano
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Steven D'Aprano
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Grant Edwards
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Random832
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,

Re: Extract the middle N chars of a string

2016-05-18 Thread MRAB
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'

Re: Extract the middle N chars of a string

2016-05-18 Thread Peter Otten
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Peter Otten
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

Re: Extract the middle N chars of a string

2016-05-18 Thread Ian Kelly
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