1

I just thinking how to convert the numbers into the ' * ' (eg. if I enter 4 then **** and the result will be like this: ****, ***, **, *) I know that the code should be like this:

number = int(input())
while number >= 0:
    print (number)
    number = number - 1

but how to make it become ' * '? Thanks.

Radosław Miernik
  • 4,004
  • 8
  • 33
  • 36
nightowl_nicky
  • 341
  • 1
  • 5
  • 14
  • 1
    Possible duplicate of [Python display string multiple times](http://stackoverflow.com/questions/963161/python-display-string-multiple-times), [In Python, how do I create a string of n characters in one line of code?](http://stackoverflow.com/questions/1424005/in-python-how-do-i-create-a-string-of-n-characters-in-one-line-of-code) – GingerPlusPlus Jan 31 '16 at 19:10

3 Answers3

4

Try this:

print(number * '*')

It will print * number times. Example:

>>> print(4 * '*')
'****'
Radosław Miernik
  • 4,004
  • 8
  • 33
  • 36
2

Another approach:

''.join('*' for _ in range(4))

However, as @GingerPlusPlus points out, this approach is slower than the other one overloading the * operator.

Pier Paolo
  • 878
  • 1
  • 14
  • 23
1

If I understand you correctly, you want to print the astericks * symbol X amount times based on the number entered and then you want to count down to 1? That explanation might be rough, so here's an example of what I believe you are asking for:

If the user enters 3, you want to print:

***
**
*

If that is correct then here's a possible implementation (coded for Python 3.x):

number = int(input())

while (number > 0):
    print( '*' * number)
    number -= 1
# End While
Spencer D
  • 3,376
  • 2
  • 27
  • 43