Exercise
Right-Aligned Triangle
Objective
Develop a Python program that prompts the user for a string and displays a right-aligned triangle:
n
an
uan
Juan
Example Python Exercise
Show Python Code
# Main program loop
while True:
user_input = input("Enter a string: ")
# Initialize variable for the length of the string
length = len(user_input)
# Loop through each character in the string
for i in range(1, length + 1):
# Create a substring and right-align it
substring = user_input[:i]
print(substring.rjust(length))
break
Output
Enter a string: Juan
J
Ju
Jua
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 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...
In this exercise, you will develop a Python program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions...
In this exercise, you will develop a Python program that creates a 70x20 two-dimensional array of characters, "draws" a circle with a radius of 8 inside it, and displ...
In this exercise, you will develop a Python program that can store up to 1,000 records of software applications. For each application, you must keep the following dat...