Python: Format a number with a percentage
Python String: Exercise-36 with Solution
Format number as percentage.
Write a Python program to format a number with a percentage.
Sample Solution:
Python Code:
# Define a variable 'x' and assign it the value 0.25 (a floating-point number).
x = 0.25
# Define a variable 'y' and assign it the value -0.25 (a floating-point number).
y = -0.25
# 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' as a percentage with two decimal places and print it.
print("Formatted Number with percentage: "+"{:.2%}".format(x))
# Print the original value of 'y' with a label.
print("Original Number: ", y)
# Format the value of 'y' as a percentage with two decimal places and print it.
print("Formatted Number with percentage: "+"{:.2%}".format(y))
# Print an empty line for spacing.
print()
Sample Output:
Original Number: 0.25 Formatted Number with percentage: 25.00% Original Number: -0.25 Formatted Number with percentage: -25.00%
Flowchart:
Python Code Editor:
Previous: Write a Python program to display a number with a comma separator.
Next: Write a Python program to display a number in left, right and center aligned of width 10.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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://www.w3resource.com/python-exercises/string/python-data-type-string-exercise-36.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics