Reputation: 868
In Python the print
function automatically takes new line after the statements, if we want to print the whole statement in a single line then what should be used?
For example:
>>> for number in range(1,6):
... for k in range (0,number):
... print ("*")
...
I got the following output:
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
While I need this one:
*
**
***
****
*****
Upvotes: 0
Views: 270
Reputation: 13943
Try the following in Python 2.x.x. You could use print "*"*i
where i is a number which prints '*' for i number of times.
print "*" * 3
Output
***
All you have to do is to choose the value of i
carefully.
for i in range(0,6): ## i increments through the range(0,6) for every iteration
print "*"*i ## "*" is printed 'i' times
Output:
*
**
***
****
*****
Upvotes: 7
Reputation:
Set the end
parameter of print
to ""
and put an extra print()
just outside the inner loop:
for number in range(1,6):
for k in range (0,number):
print ("*", end="")
print() # This is needed to break up the lines.
Below is a demonstration:
>>> for number in range(1,6):
... for k in range (0,number):
... print ("*", end="")
... print()
...
*
**
***
****
*****
>>>
Upvotes: 6
Reputation: 14429
Use a comma after print:
>>> for number in range(1,6):
... for k in range (0,number):
... print ("*"),
... print()
Upvotes: 1