Python Practice › Loop Programs › Program 41

Letter T pattern

MediumLoop Programs · 41 of 90

Write a Python program to print the letter ‘T’ with stars: two full rows of 9 stars and a 3-star stem in the middle.

Advertisement
Output
* * * * * * * * * 
* * * * * * * * * 
      * * * 
      * * * 
      * * * 
      * * * 
      * * * 

Approach

  1. Print the top bar twice, then the stem rows padded with spaces to centre them.

Solution

Python
width, stem, height = 9, 3, 5for _ in range(2):    print("* " * width)pad = (width - stem) // 2for _ in range(height):    print("  " * pad + "* " * stem)
Output
* * * * * * * * * 
* * * * * * * * * 
      * * * 
      * * * 
      * * * 
      * * * 
      * * * 

Line by line

Line 3

"* " * 9 repeats the star-space pair 9 times.

Line 4

The stem starts 3 star-widths in, so it sits in the centre.

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs

Leave a Comment