Home > Enterprise >  I am unable to run this command in (.format) in Python .... please guide me the correct synatx with
I am unable to run this command in (.format) in Python .... please guide me the correct synatx with

Time:03-01

a = 12345.98521466
print('The value of a is :{}'.format(a:0.3f))

This code is giving syntax error.What is the correct syntax for printing upto 3 decimal places in format()?

CodePudding user response:

There will be serveral ways. I provided two ways to print the value using format, as follows:

a = 12345.98521466

# solution 1: using f-string
print(f'The value of a is :{a:0.3f}')  # The value of a is :12345.985

# solution 2: using .format
print('The value of a is :{:0.3f}'.format(a))  # The value of a is :12345.985

For more information about printing foramt, please refer to: https://docs.python.org/3/tutorial/inputoutput.html

  • Related