TextWrap: How can I remove the none at the end?

You are given a string S and width w.
Your task is to wrap the string S into a paragraph of width w

Input Format

The first line contains a string S,
The second line contains the width w,

Output Format

Print the text wrapped paragraph.

Sample Input 0

ABCDEFGHIJKLIMNOQRSTUVWXYZ
4

Sample Output 0

ABCD
EFGH
IJKL
IMNO
QRST
VWX
YZ

My Code:

import textwrap
import math

def wrap(string, max_width):
    x=list(string)
    y=len(x)
    p=0

    for i in range((math.ceil(y/max_width))):
        for j in range(max_width):
            try:
                print(x[p],end='')
                p+=1
            except:
                pass
        if p<y:
            print('\t')
        else:
            pass

if __name__ == '__main__':
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)

The problem with these is that in question I can use the textwrap module, but I tried solve this problem without it but why am I getting the none at the end, I don’t understand that part

My Output:
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZNone

The None statement is being returned from the first for loop as far as I can tell.

When I added an explicit return statement, None was removed and replaced with the contents of the return.

But I can’t get my head around why either.

2 Likes

Yes, you assign the return value of your wrap function to the variable result, but that function doesn’t have an explicit return statement so the value is None.

All the printing is done by the function itself. You could just do wrap(string, max_width) at the end.

2 Likes