one-hand-octopus
one-hand-octopus

Reputation: 2743

How to wrap from the other end with textwrap?

wrap in textwrap wraps from left to right:

from textwrap import wrap
print(wrap('1100010101', 4))

returns: ['1100', '0101', '01']

Is there a way to wrap it from right to left, so it returns: ['11', '0001', '0101']?

Upvotes: 0

Views: 75

Answers (1)

I'mahdi
I'mahdi

Reputation: 24049

You can first reverse '1100010101' then reverse each element and at end reverse result of wrap like below:

>>> from textwrap import wrap
>>> st = '1100010101'[::-1]
>>> list(map(lambda x: x[::-1] , wrap(st, 4)))[::-1]
['11', '0001', '0101']

Upvotes: 2

Related Questions