Python’s many ways of string formatting — are the older ones (going to be) deprecated?

The new .format() method is meant to replace the old % formatting syntax. The latter has been de-emphasised, (but not officially deprecated yet). The method documentation states as much: This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in … Read more

Display number with leading zeros

In Python 2 (and Python 3) you can do: number = 1 print(“%02d” % (number,)) Basically % is like printf or sprintf (see docs). For Python 3.+, the same behavior can also be achieved with format: number = 1 print(“{:02d}”.format(number)) For Python 3.6+ the same behavior can be achieved with f-strings: number = 1 print(f”{number:02d}”)