Python: Display a number with a comma separator
Python String: Exercise-35 with Solution
Write a Python program to display a number with a comma separator.
Sample Solution:
Python Code:
# Define a variable 'x' and assign it the value 3,000,000 (an integer).
x = 3000000
# Define a variable 'y' and assign it the value 30,000,000 (an integer).
y = 30000000
# 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 a comma separator for thousands and print it.
print("Formatted Number with comma separator: "+"{:,}".format(x))
# Print the original value of 'y' with a label.
print("Original Number: ", y)
# Format the value of 'y' with a comma separator for thousands and print it.
print("Formatted Number with comma separator: "+"{:,}".format(y))
# Print an empty line for spacing.
print()
Sample Output:
Original Number: 3000000 Formatted Number with comma separator: 3,000,000 Original Number: 30000000 Formatted Number with comma separator: 30,000,000
Flowchart:
Python Code Editor:
Previous: Write a Python program to print the following integers with '*' on the right of specified width.
Next: Write a Python program to format a number with a percentage.
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://198.211.115.131/python-exercises/string/python-data-type-string-exercise-35.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics