Pad number

Python, String, Math · Nov 2, 2020

Pads a given number to the specified length.

  • Use str.zfill() to pad the number to the specified length, after converting it to a string.
def pad_number(n, l):
  return str(n).zfill(l)
pad_number(1234, 6); # '001234'

More like this

  • Python Math

    Learn how to perform common mathematical operations in Python 3.6 with this snippet collection.

    Collection · 37 snippets

  • Pad string

    Pads a string on both sides with the specified character, if it's shorter than the specified length.

    Python, String · Oct 3, 2020

  • Repeat string

    Generates a string with the given string value repeated n number of times.

    Python, String · Nov 2, 2020

  • Hex to RGB

    Converts a hexadecimal color code to a tuple of integers corresponding to its RGB components.

    Python, String · Sep 15, 2020