Exercise
Symmetrical Triangle
Objective
Develop a Python program that displays a centered triangle from a string entered by the user:
a
uan
Juan
Example Python Exercise
Show Python Code
# Prompt the user for their name
name = input("Enter your name: ")
# Calculate the middle index of the string
length = len(name)
# Loop to print each row, progressively increasing the number of characters
for i in range(1, length + 1):
# Center-align the substring by padding with spaces
print(name[:i].center(length))
Output
Enter your name: Juan
J
Juan
Juan
Share this Python Exercise
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.
In this exercise, you will develop a Python program to create a database for storing information about urban areas. In the first approach, you will store only the nam...
In this exercise, you will develop a Python program to mimic the basic Unix SysV "banner" utility, capable of displaying large texts. This exercise is perfect ...
In this exercise, you will develop a Python program that prompts the user for a string and displays a right-aligned triangle. This exercise is perfect for prac...
In this exercise, you will develop a Python program that prompts the user for a string and performs several transformations. The program will replace all lowercase 'a...
In this exercise, you will develop a Python program to store two pieces of data for a person: Name and Date of birth. The Date of birth must be a...
In this exercise, you will develop a Python program that prompts the user for 10 integer numbers (ranging from -1000 to 1000), sorts them, and displays them in sorted...