Triangular Shape - Python Programming Exercise

This Python program prompts the user to input a symbol and a width, then displays a triangle of the specified width using that symbol for the inner part. The program constructs the triangle by printing rows of the symbol, starting with a width equal to the user's input and decreasing by one symbol per row. This demonstrates the use of loops in Python to control the number of symbols printed on each line, making it a great example of repetition and string manipulation. By using the for loop and string operations, this program allows users to dynamically create patterns based on their input. The width of the triangle decreases with each iteration of the loop, showcasing how nested structures can be used to generate complex visual outputs in Python. This simple yet effective implementation highlights how to manipulate strings and control output formatting efficiently, making it a valuable learning tool for those interested in mastering Python programming.

 Category

Python Data Types

 Exercise

Triangular Shape

 Objective

Develop a Python program that prompts the user for a symbol and a width, and displays a triangle of that width using that symbol for the inner part, as in this example:

Enter a symbol: 4
Enter the desired width: 5

44444
4444
444
44
4

 Example Python Exercise

 Copy Python Code
# Prompt the user for a symbol and width
symbol = input("Enter a symbol: ")
width = int(input("Enter the desired width: "))

# Display the triangle with the given symbol and width
for i in range(width, 0, -1):
    print(symbol * i)

 Output

Case 1:
Enter a symbol: 4
Enter the desired width: 5
44444
4444
444
44
4

Case 2:
Enter a symbol: *
Enter the desired width: 3
***
**
*

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  String Password

    This Python program prompts the user for a username and a password (both should be strings) and repeats the process until the correct credentials are en...

  •  Arithmetic - Conditional

    This Python program prompts the user for two numbers and an operation to perform on them, such as addition (+), subtraction (-), multiplication (*), or divisio...

  •  Double Value

    This Python program calculates the perimeter, area, and diagonal of a rectangle, based on the given width and height. The perimeter is cal...

  •  Evaluate Function Outputs

    This Python program displays the values of the function y = x² - 2x + 1 for integer values of x ranging from -10 to +10. The function is a quadratic equation, ...

  •  Show Function

    This Python program "draws" the graph of the function y = (x-4)² by displaying a series of asterisks on the screen. For each x value ranging from -1 to 8, the ...

  •  Floating Point, Speed Units

    This Python program prompts the user to input a distance in meters and the time taken in hours, minutes, and seconds, and then calculates the speed in three different units:...