Shakib
Shakib

Reputation: 27

how to remove a space from a line

My program is printing an extra space after every line. I want to remove the extra space from end of each line.

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

Input: 3 99

Output:

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

After end of the each line I am printing an extra space, How can I remove this space?

Upvotes: 1

Views: 69

Answers (3)

HackLab
HackLab

Reputation: 539

Just Iterate over one less and then output with normal end instead of empty print

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 - 1)
    count+=1

Upvotes: 0

cdlane
cdlane

Reputation: 41925

How about a simpler approach where we print the numbers as a group and control both separator and end character:

x, y = map(int, input().split())

count = 0

while count < y // x:
    start = count * x + 1
    end = start + x

    print(*range(start, end), sep=" ")

    count += 1

Upvotes: 2

Mayank Singhal
Mayank Singhal

Reputation: 107

Make a temporary array variable to store your values. Then print the array in one go as shown here.

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

Upvotes: 0

Related Questions