Can't understand arithmetic formatter errors

Tell us what’s happening:
I can’t grasp what is wrong here, it prints exactly how it should (at least I think so) but I get error reports about improper formatting. I’ve looked through the forum and found tips about error results and what they mean, but I still don’t understand.

Your code so far

def arithmetic_arranger(problem, solve=False):
    up = ""
    down = ""
    wynik=""
    odp=""
    dashes=""
    drow=""

    ile = len(problem)
    if ile>5:
        return "Error: Too many problems."
    for dzialanie in problem:
        dzialanie=dzialanie.split()
        first=dzialanie[0]
        second=dzialanie[2]
        sign=dzialanie[1]
        if sign != "-" and sign != "+":
            return "Error: Operator must be '+' or '-'."
        if len(first)>4 or len(second)>4:
            return "Error: Numbers cannot be more than four digits."
        if first.isdecimal() is False or second.isdecimal() is False:
            return "Error: Numbers must only contain digits."
        lenght =max(len(str(first)), len(str(second))) + 2
        top =str(first).rjust(lenght)
        bottom =sign + str(second).rjust(lenght - 1)
        up +=top + '    '
        down +=bottom + '    '
        if sign=="+":
            wynik=int(first) + int(second)
        if sign=="-":
            wynik=int(first) - int(second)
        result = str(wynik).rjust(lenght)
        odp += result + '    '
        if len(str(bottom)) > len(str(top)):
            if len(str(bottom)) > len(str(result)):
                for i in range (len(str(bottom))):
                    dashes = dashes + "-"
            else:
                for i in range (len(str(wynik))):
                    dashes = dashes + "-"
        else:
            for i in range(len(str(top))):
                dashes = dashes + "-"
        drow += dashes + '    '
        dashes=""
    if solve is True:
        arranged_problems =up + '\n' + down + '\n' + drow + '\n' + odp
    else:
        arranged_problems =up + '\n' + down + '\n' + drow
    return arranged_problems

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Arithmetic Formatter

Link to the challenge:

Let’s look at the first failing test.

The expected output is the following:

"  3801      123\n-    2    +  49\n------    -----"

Instead you are outputting the following:

"  3801      123    \n-    2    +  49    \n------    -----    "

I added the quotation marks around the strings to make it easier to see the extra space characters you are adding. Looking at the console at a glance you would not notice these extra characters but they are there.