w3resource

Python: Display a number in left, right and center aligned of width 10

Python String: Exercise-37 with Solution

Write a Python program to display a number in left, right, and center aligned with a width of 10.

Python String Exercises: Display a number in left, right and center aligned of width 10

Sample Solution:

Python Code:

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

# Print an empty line for spacing.
print()

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

# Format and print the value of 'x' with left alignment, a width of 10 characters, and padding using spaces.
print("Left aligned (width 10)   :"+"{:< 10d}".format(x))

# Format and print the value of 'x' with right alignment, a width of 10 characters, and padding using spaces.
print("Right aligned (width 10)  :"+"{:10d}".format(x))

# Format and print the value of 'x' with center alignment, a width of 10 characters, and padding using spaces.
print("Center aligned (width 10) :"+"{:^10d}".format(x))

# Print an empty line for spacing.
print()

Sample Output:

Original Number:  22                                                                                          
Left aligned (width 10)   :22                                                                                 
Right aligned (width 10)  :        22                                                                         
Center aligned (width 10) :    22  

Flowchart:

Flowchart: Display a number in left, right and center aligned of width 10

Python Code Editor:

Previous: Write a Python program to format a number with a percentage.
Next: Write a Python program to count occurrences of a substring in a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/string/python-data-type-string-exercise-37.php