Skip to content

MicroPython String Formatting

The normal Python has advanced string formatting functions using "%" and .format methods documented at the PyFormat website.

Although most of these functions work, there are some exceptions when using date formats.

The following % formats do not work under MicroPython:

  1. %Y - year
  2. %m - month
  3. %d - day of month
  4. %H - hour of day
  5. %M - minute

Padding Example

The following example prints a floating point number with two decimals of precision in a field of six characters with leading zeros.

1
print('{:06.2f}'.format(3.141592653589793))

returns:

1
003.14