python中出现了不需要的换行符

python中出现了不需要的换行符,python,python-3.x,string,Python,Python 3.x,String,我想在使用textwrap时替换字符串的特定部分。但我有一个不必要的线路中断。我已经尝试了很多方法,但仍然不起作用。这是我的密码 import textwrap import math def wrap(string, max_width): wrapper = textwrap.TextWrapper(width = max_width) word_list = wrapper.fill(text = string)

我想在使用
textwrap
时替换字符串的特定部分。但我有一个不必要的线路中断。我已经尝试了很多方法,但仍然不起作用。这是我的密码

    import textwrap
    import math
    
    def wrap(string, max_width):
        wrapper = textwrap.TextWrapper(width = max_width)
        word_list = wrapper.fill(text = string)
        return word_list
    
    height, width = map(int,input().split())
    square = height * width
    
    i = 0
    x = []
    while i < square:
        x.append("-")
        i += 1
    k = 0
    
    while k < math.floor(height / 2):
            line = math.floor(width / 2) + width * k
            x[line] = '|'
            x[line + 1]= "."
            x[line - 1] = "."
            for h in range(1, k + 1):
                f = h * 3
    
                line_plus = line + f
                line_minus = line - f
                x[line_plus] = '|'
                x[line_minus] = '|'
                x[line_minus - 1] = '.'
                x[line_plus - 1] = '.'
                x[line_minus + 1] = '.'
                q = line_plus + 1
                x[q] = '.'
    
            k += 1
    a = 0
    while a < math.floor(height / 2):
        line = math.floor(width / 2) + width * a
        line_end = (math.floor(width / 2) + width * a) * (-1)
        x[line_end - 1] = '|'
        if line > width:
            x[line_end + 2] = '|'
            x[line_end - 4] = '|'
        a += 1
    
    listToStr = ''.join([str(elem) for elem in x])
    
    welcome_pos = math.floor(height / 2) * width + (math.floor(width / 2) - math.floor(7 / 2))
    
    s = listToStr[ 0: welcome_pos] + "welcome" + listToStr[welcome_pos + 7:]
       
    print(wrap(listToStr, width) + "\n")
    print(wrap(s, width))

这是我的输出。但它在第3行提供了一个我不想要的空间。我不知道为什么会这样。请帮帮我。

也许这就是你想要的

代码:

结果:

5 4
-..|..|..||--|--||--
-..|..|welcome--||--
#(above the same)
print(wrap(listToStr,width*height))
print(wrap(s,width*height))
5 4
-..|..|..||--|--||--
-..|..|welcome--||--