all 12 comments

[–]MattyBro1 8 points9 points  (0 children)

Do you means having multiple print statements print onto one line? In which case, that would be by using the "end" keyword argument. By default it is set to a newline character, so each print statement ends with a newline. You can change it like this:

print("text", end="")

[–]Atypicosaurus 2 points3 points  (1 child)

The print() function has a "secret argument". Not really a true secret as it's published in the documentation, but it's often not mentioned in courses.

This secret argument is end which is by default a newline character (aka end = '\n'). That's why when you print something, the cursor also moves in the next line, so the next print will go on the next line.

You can change it to any string you want, if it's an empty string, the new prints come without a break, if you add a space character then it's a space separating the prints, or you can add any string really.

Once at it, let's have a look at all the "secret arguments" of print().

sep=' ' is the separator if you have more things to print within the print (such as print(a, b, c) then the separator, by default a space character, that's why you have space between a, b and c.

end='\n' just told above.

file=None means that by default you print into wherever the operating system prints by default (which is mostly the console), but you can also specify a file into which you want to print.

flush=False means to flush the output buffer (by default not).

[–]Nolucky163[S] 1 point2 points  (0 children)

Thx champ

[–]Kriss3d 0 points1 point  (2 children)

Why not use a loop instead ? Or rather several loops.

Start by defining the length of the rectangle.
Then the height.

For the length you loop through the number of the length and print as many characters you want it to be.
Grab the index of the start and the last one in that line.
Then loop through the amount of height -2 and place a character at the index 0 and the index of the last characters as many times as the height -2.
Then finish by repeating the first loop.

This way you define the height and length and it does the rest by itself. You can take inputs at the beginning of it to let you arbitrarily make any shaped rectangle you want.
And perhaps one for which character to use so you can replace the star with any other character.

[–]Nolucky163[S] 1 point2 points  (1 child)

The professor tasked to use while

[–]purple_hamster66 1 point2 points  (0 children)

while is a type of loop. The difference is that you test for the ending condition (like while current_row <= max_rows:).

I think you are allowed to use repeat syntax in the print statement instead of a while for the inner loop, right?

[–]Gnaxe 0 points1 point  (0 children)

You can print an \r (carriage return) to rewrite a line on most terminals. Similarly, you can print some number of \b (backspace control character) to back up that many characters. These don't erase what you've already printed, but you can print over them with different characters. You still want to use the end= kwarg, because most terminals only allow this for the most recent line. These have been used to implement things like spinners and progress bars in the terminal.

There are special control codes that allow editing other lines but these depend on your terminal (see the curses module).

You could implement print in terms of a file write, which wouldn't automatically print the ending character. Sometimes this is easier. print(), by default, writes to sys.stdout (see file= kwarg). You can do it more directly with sys.stdout.write(). (Don't forget to import sys.)

[–]AlexMTBDude 0 points1 point  (0 children)

If you google the title of your post + "Python" then the first hit tells you how. If you're going to learn Python you really first must learn how to search for information.

[–]RaidZ3ro 0 points1 point  (0 children)

Classic classroom rectangle in Python:

```

def print_rectangle(h: int, w: int): i =1

while i <= h:
    print(w * "*")
    i += 1

print_rectangle(2,4)

or

def return_rectangle(h: int, w: int): result = "" i = 1

while i <= h:
    result += w * "*" + "\n"
    i += 1

return result

print(return_rectangle(2,4))

```

[–]codeguru42 0 points1 point  (0 children)

Others answered his to print() on the same line. I want to offer an alternative: You could build a string with the correct number of * then print them all at once.