如何从行中删除空格

问题描述 投票:0回答:1

我的程序在每一行之后都打印了多余的空格。我想删除每行末尾的多余空间。

x, y = map(int, input().split())
count = 0
while count < (y//x):
    start = count*x + 1
    end = (count + 1) * x + 1
    for i in range(start,end,1):
        print(i,end=" ")
    print("")
    count+=1

输入: 3 99

输出:

1 2 3 
4 5 6
7 8 9
.....

每行结束后,我要打印一个多余的空格,如何删除该空格?

python-3.x printing output
1个回答
0
投票

仅迭代一遍,然后以正常结束输出而不是空白打印

x, y = map(int, input().split())
count = 0
while count < (y//x):
    start = count*x + 1
    end = (count + 1) * x + 1
    for i in range(start,end - 1,1):
        print(i,end=" ")
    print(end)
    count+=1
© www.soinside.com 2019 - 2024. All rights reserved.