w3resource

Python: Print the following integers with '*' on the right of specified width


Print integers with * right-padded.

Write a Python program to print the following integers with '*' to the right of the specified width.

Python String Exercises: Print the following integers with '*' on the right of specified width

Sample Solution:

Python Code:

# Define a variable 'x' and assign it the value 3 (an integer).
x = 3

# Define a variable 'y' and assign it the value 123 (an integer).
y = 123

# Print an empty line for spacing.
print()

# Print the original value of 'x' with a label.
print("Original Number: ", x)

# Format the value of 'x' with right padding using asterisks and a width of 3 characters, and print it.
print("Formatted Number (right padding, width 2): "+"{:*< 3d}".format(x))

# Print the original value of 'y' with a label.
print("Original Number: ", y)

# Format the value of 'y' with right padding using asterisks and a width of 7 characters, and print it.
print("Formatted Number (right padding, width 6): "+"{:*< 7d}".format(y))

# Print an empty line for spacing.
print()

Sample Output:

Original Number:  3                                                                                           
Formatted Number(right padding, width 2): 3*                                                                  
Original Number:  123                                                                                         
Formatted Number(right padding, width 6): 123*** 

Flowchart:

Flowchart: Print the following integers with ‘*’ on the right of specified width

For more Practice: Solve these Related Problems:

  • Write a Python program to right-pad a list of integers with '*' characters to a specified width using str.ljust().
  • Write a Python program to format numbers so that they have a fixed width with trailing asterisks.
  • Write a Python program to use f-strings to display integers padded on the right with '*' to a given total width.
  • Write a Python program to implement a function that takes a number and returns it as a string padded on the right with '*' characters.

Go to:


Previous: Write a Python program to print the following integers with zeros on the left of specified width.
Next: Write a Python program to display a number with a comma separator.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.