Search code examples
pythonformattingdigits

Specifying digits (without decimal) in Python


I'm trying to specify the number of digits (not decimal places) for a number output. Specifically, I'm trying to make the output be a three digit number. For example: If a user inputs the number 7, the output is then 007.


Solution

  • >>> i = 7
    >>> str(i).rjust(3, '0')
    '007'
    

    rjust docs