Product SiteDocumentation Site

6.5. Some printing * examples

Here are some examples which you can find very often in college lab reports

Design 1
#!/usr/bin/env python
row = int(raw_input("Enter the number of rows: "))
n = row
while n >= 0:
    x =  "*" * n
    print x
    n -= 1

The output
$ ./design1.py
Enter the number of rows: 5
*****
****
***
**
*

Design 2
#!/usr/bin/env python
n = int(raw_input("Enter the number of rows: "))
i = 1
while i <= n:
    print "*" * i
    i += 1

The output
$ ./design2.py
Enter the number of rows: 5
*
**
***
****
*****

Design 3
#!/usr/bin/env python
row = int(raw_input("Enter the number of rows: "))
n = row
while n >= 0:
    x =  "*" * n
    y = " " * (row - n)
    print y + x
    n -= 1

The output
$ ./design3.py
Enter the number of rows: 5
*****
 ****
  ***
   **
    *