Home > OS >  How is print('\r') or print(' ') giving me the output?
How is print('\r') or print(' ') giving me the output?

Time:11-17

We were asked to print the following output:

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

I understand that it would require two loops so I tired this:

a = int(input())
i = a
f = 1
while i>0:
 for j in range(i):
      print(f,end=' ')
 f  = 1
 i -= 1
 print('\r')

With this I am getting the desired output, but as soon as I remove the last line of print('\r') the output becomes something like this:

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

The desired output also comes out when I used print(' ') instead of print('\r'), I don't understand why this is happening?

Ps: I am a noob coder, starting my freshman year, so please go easy on me, if the formatting is not up to the mark, or the code looks bulky.

CodePudding user response:

Probably not helping you so much but the following code produces the expected output:

a = 10
for i, j in enumerate(range(a, 0, -1), 1):
    print(*[i] * j)

# Output:
1 1 1 1 1 1 1 1 1 1   # i=1, j=10
2 2 2 2 2 2 2 2 2     # i=2, j=9
3 3 3 3 3 3 3 3       # i=3, j=8
4 4 4 4 4 4 4         # i=4, j=7
5 5 5 5 5 5           # i=5, j=6
6 6 6 6 6             # i=6, j=5
7 7 7 7               # i=7, j=4
8 8 8                 # i=8, j=3
9 9                   # i=9, j=2
10                    # i=10, j=1

The two important parameters here are sep (when you print a list) and end as argument of print. Let's try to use it:

a = 10
for i, j in enumerate(range(a, 0, -1), 1):
    print(*[i] * j, sep='-', end='\n\n')

# Output:
1-1-1-1-1-1-1-1-1-1

2-2-2-2-2-2-2-2-2

3-3-3-3-3-3-3-3

4-4-4-4-4-4-4

5-5-5-5-5-5

6-6-6-6-6

7-7-7-7

8-8-8

9-9

10

Update

Step by step:

# i=3; j=8

>>> print([i])
[3]

>>> print([i] * j)
[3, 3, 3, 3, 3, 3, 3, 3]

# print takes an arbitrary number of positional arguments.
# So '*' unpack the list as positional arguments (like *args, **kwargs)
# Each one will be printed and separated by sep keyword (default is ' ')
>>> print(*[i] * j)

CodePudding user response:

To make it all easier and prevent errors, you can simply do this:

n = 10
for i in range(1, n   1):
    txt = str(i)   " " # Generate the characters with space between
    print(txt * (n   1 - i)) # Print the characters the inverse amount of times i.e. 1 10, 10 1

Where it generates the text which is simply the number a space, then prints it out the opposite amount of times, (11 - current number), i.e. 1 ten times, 10 one time.

CodePudding user response:

I suggest using 2 or 4 spaces for indenting. Let's take a look:

a = int(input())
i = a
f = 1
while i>0:
    for j in range(i):
        print(f,end=' ')
    f  = 1
    i -= 1
    print('\r')

Notice the print(f,end=' ') within the inner loop. the end=' ' bit is important because print() appends a trailing new line \n to every call by default. Using end=' ' instead appends a space.

Now take a look at print('\r'). It does not specify end=' ', so it does still append a newline after each call to print. The fact that you additionally print a \r is inconsequential in this case. You could also just do print().

CodePudding user response:

you can do this way :

rows = 10
b = 0

for i in range(rows, 0, -1):
    b  = 1
    for j in range(1, i   1):
    print(b, end=' ')
    print('\r')

CodePudding user response:

Code:-

  • A basic way to do it:-
for i in range(1,11):
    for j in range(11,i,-1):
        print(i,end=' ')
    print()

CodePudding user response:

No need for multiple loops.

for i in range(1,11):
    # concatenate number   a space repeatedly, on the same line
    # yes, there is an extra space at the end, which you won't see ;-)
    print(f"{i} " * (11-i))

output:

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

As to what's happening with your code...

A basic Python print prints on a line, meaning that it ends with a line feed (which moves it to the next line).

So, if I take your word for it, you've done all the hard work of say the first line of 10 ones with spaces, when you are done at the following point.

#your code
 f  = 1
 i -= 1

Now, so far you've avoided that line feed by changing the end parameter to print so that it doesn't end with a newline. So you have:

1 1 1 1 1 1 1 1 1 1

And still no line feed. Great!

But if you now start printing 2 2 2 2 2 2 2 2 2 , it will just get added to... the end of the previous line, without line feed.

So to force a line feed, you *print anything you want, but without the end parameter being set, so that print now ends with the linefeed it uses by default.

Example:

#without line feed
print("1 " * 3, end=' ')
print("2 " * 2, end=' ')

output:

1 1 1  2 2

Lets try printing something, anything, without a end = ' ')

print("1 " * 3, end=' ')
#now add a line by a print that is NOT using `end = ' '`
print("!")
print("2 " * 2, end=' ')

output:

1 1 1  !
2 2

OK, so now we have a line feed after ! so you jump to the next line when printing the 2s. But you don't want to see anything after the 1s.

Simples, print anything that is invisible.

print("1 " * 3, end=' ')
#now add a line by a print, but using a non-visible character.
#or an empty string.  Tabs, spaces, etc... they will all work
print(" ")
print("2 " * 2, end=' ')

output:

1 1 1
2 2

This would also work:

print("1 " * 3, end=' ')
#we could also print a linefeed and end without one...
print("\n", end="")
print("2 " * 2, end=' ')

  • Related