146k views
4 votes
How to print in this on python
#
# #
# # #
# # # #
# # # # #

User RcMan
by
4.5k points

2 Answers

4 votes

Answer:

triangular ##&((+ hhvbh. hhhb hhv

User Eric Parshall
by
3.7k points
3 votes

Answer:

for i in range(1, 6):

print(("# " * i).rstrip())

Step-by-step explanation:

You need to print one hashtag, then 2, then 3, and so on. We can use Python's string multiplication to get each line quicker.

String multiplication is an easy way to repeat the same string a certain number of times. It's done by multiplying the string to a number.

For example,

"hello" * 6

>> "hellohellohellohellohellohello"

If we multiply the string "#" by any number, we would get a number of hashtags side by side, without any space. To easily avoid this, we can multiply "# " instead to automatically put a space to the right of each hashtag.

We can use a for loop that goes through range(1, 6) to print out 5 rows.

for i in range(1, 6):

Then, we can print the string "# " multiplied by i.

print("# " * i)

However, this will also put a space at the end of the string, which we do not want to print. Hence, we can use the .rstrip() method to strip the string of any spaces on the right. We will first put the whole string in parentheses, then invoke rstrip on it.

print(("# " * i).rstrip())

Let's put both lines together.

for i in range(1, 6):

print(("# " * i).rstrip())

User Vel
by
4.6k points