>>> a = '{"key1": "aaaaaaaaaaaaaaaaa", "key2": "bbbbbbbbbbbbbbbbbbbbbbbb"}'
>>> len(a)
64
>>> textwrap.wrap(a, 32, drop_whitespace=False)
['{"key1": "aaaaaaaaaaaaaaaaa", ', '"key2": ', '"bbbbbbbbbbbbbbbbbbbbbbb"}']
I was expecting
['{"key1": "aaaaaaaaaaaaaaaaa", "k', 'ey2": "bbbbbbbbbbbbbbbbbbbbbbb"}']
I'am missing something ?
Your expectation is wrong, according to the official documentation:
Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.
[...]
Text is preferably wrapped on whitespaces and right after the hyphens in hyphenated words; only then will long words be broken if necessary, unlessTextWrapper.break_long_words
is set to false.
Your expected output is literally broken off after 32 characters, whereas the actual output is split into segments of 30, 8, and 27 characters long – broken only on the whitespace characters in the original string.
The second segment is much shorter than the others because the first string plus this next non-whitespace run "key2":
is longer than 32 characters, and this short run plus the next phrase is also longer than 32 characters. Only when there is absolutely no possibility to break on a space or hyphen, a break in the middle of a non-whitespace run will occur.