How to easily print triangle patterns in python – Programming Code Examples
Python

How to easily print triangle patterns in python2 min read

Printing of patterns is one of the toughest jobs for the beginners and I am gonna show you how to easily print patterns using some basic concepts of text alignment in python.
In Python, a string of text can be aligned left, right and center by use of following functions.

.ljust(width)




This method returns a left aligned string of length width.

Output:

>>Spiderlabweb——-

As you can see that Spiderlabweb is shifted to left by 3 places because length of ‘Spiderlabweb ‘ is 12 and 15-12=3

.center(width)

This method returns a centered string of length width.

Output:

>>–Spiderlabweb–

As you can see that Spiderlabweb is shifted to 2 places to the right and 2 places to the left because  16-12=4 therfore 2 from left and two from right

.rjust(width)

This method returns a right aligned string of length width.

Output:

>>—Spiderlabweb

Similar to .ljust(width)

Right Angle Triangle Pattern:

The pattern in which one side is perpendicular to other and here is the code:

Output:

 

Inverted Right Angle Triangle Pattern:

The inverted pattern for right angle triangle :

Output:

 

Isosceles Triangle Pattern:

An isosceles triangle has two sides equal and here is the code for it:

Output:

 

Inverted Isosceles Triangle Pattern:

The  code for it is :

Output:

For more Python Examples click on the link below:

Python Examples

Leave a Comment