I have this function, but I am getting this error. I have tried but am not able to fix this.
size -= len(prefix)
if size % 2:
size -= 1
return '\n'.join([prefix + line for line in textwrap.wrap({size, string})])
It seems like you're trying to do something like this:
import textwrap
s = 'abcdefghijklmnopqrstuvwxyz'
prefix = '>>> '
desired_width = 15
width_without_prefix = desired_width - len(prefix)
print('\n'.join(prefix + l for l in textwrap.wrap(s, width_without_prefix)))
which returns:
>>> abcdefghijk
>>> lmnopqrstuv
>>> wxyz
However, you are passing {size, string}
as a parameter to the textwrap.wrap method while the wrap method expects the first parameter to be a string:
textwrap.wrap(text, width=70, *, initial_indent='', subsequent_indent=''
To illustrate this we can check the type of the object you are passing (which is a set object):
v = {s, width_without_prefix}
print(type(v))
returns <class 'set'>
.
As it happens, this can all be simplified if you use textwrap.fill
instead of wrap
:
print(textwrap.fill(s, initial_indent=prefix, subsequent_indent=prefix, width=desired_width))