1
import textwrap
def wrap(string, max_width):
    i = textwrap.wrap(string, width=max_width)
    print(i)
    z = print(*i, sep='\n')
    return z

string, max_width = 'ABCFSAODJIUOHFWRQIOJAJ', 4 
result = wrap(string, max_width) 
print(result)

I am trying to write a code that separates a string in a list, and then prints out each part of the list as a separate line. It works fine until the last bit, where it still attaches None after running the code. I have tried all sorts of ways, but I cannot seem to force my definition to avoid the None.

2 Answers2

0

print returns None, and by returning it, your wrap function would also return None. It seems like you want to join i with a newline:

def wrap(string, max_width):
    i = textwrap.wrap(string, width=max_width)
    return '\n'.join(i)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
-1

you can use the function without return , try this instead

def wrap(string, max_width):
   i = textwrap.wrap(string, width=max_width)
   print(i)
   print(*i, sep='\n')
   

string, max_width = 'ABCFSAODJIUOHFWRQIOJAJ', 4 
wrap(string, max_width)  ``` 





wiamch
  • 11
  • 2