Defined function keeps returning None, despite having a print() specified within the return line

Question:

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.

Asked By: Complete Idiot

||

Answers:

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)
Answered By: Mureinik

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)  ``` 





Answered By: wiamch
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.