Extract the middle N chars of a string

Steven D'Aprano steve at pearwood.info
Wed May 18 21:37:57 EDT 2016


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 results as your implementation though.
> 
> The extra line should fix that, but it looks, err -- odd.

Nice! Thank you. I like this solution.



-- 
Steven




More information about the Python-list mailing list