w3resource

Python: Display a number with a comma separator


Display number with comma separator.

Write a Python program to display a number with a comma separator.

Python String Exercises: 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:

Flowchart: Display a number with comma separator

For more Practice: Solve these Related Problems:

  • Write a Python program to format a large number with commas as thousands separators using format specifiers.
  • Write a Python program to convert an integer into a string with commas inserted at every three digits.
  • Write a Python program to use locale settings to display a number with a comma separator.
  • Write a Python program to implement custom logic that inserts commas into a number string for proper digit grouping.

Go to:


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.

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.