PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] how to remove a space from a line

 August 18, 2022     output, python, python-3.x     No comments   

Issue

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?


Solution

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


Answered By - cdlane
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing