1

I am trying to print whole numbers (number left of decimal point) with the following code:

print "Completed in %g".format(str(time_difference / 60), 1.0) + " minutes, %g".format(time_difference % 60, 1.0) + " seconds."

Instead I get:

Completed in %g minutes, %g seconds.
Stunner
  • 12,025
  • 12
  • 86
  • 145

2 Answers2

2

You are mixing ways to print strings. To use format, refer to the doc here. You have to use {}. For instance:

>>> print "Completed in {0:g} minutes, {1} seconds".format(time_difference % 60, 1.0)
Completed in 40 minutes, 1.0 seconds

To use the old notation, you have to use % to format. See string formatting:

>>> print "Completed in %g minutes, %g seconds" % (time_difference % 60, 1.0)
Completed in 40 minutes, 1 seconds
fredtantini
  • 15,966
  • 8
  • 49
  • 55
1

Try this:

print "Completed in {} minutes, {} seconds.".format(int(time_difference / 60), int(time_difference % 60))

Read more about the "format" statement in the Python documentation.

Also, avoid concatenating strings using "+". It is usually more efficient to join list of strings.

Community
  • 1
  • 1