Extract the middle N chars of a string

Peter Otten __peter__ at web.de
Wed May 18 12:54:56 EDT 2016


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.




More information about the Python-list mailing list