3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by vegaseat
1

You can use one of the built-in string functions ...

n = 2
digits = 3
ns = str(n).rjust(digits, '0')

print(ns)  # 002

Edited by vegaseat: n/a

0

Thank you vegaseat and gribouillis. That's exactly what I wanted to know. I'm using vegaseat's method for now.

Cheers.

0

There is an even more specific string function ....

n = 2
digits = 3
ns = str(n).zfill(digits)

print(ns)  # 002

These string functions work with Python2 and Python3.

Gribouillis, thanks for the Python3 code.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.