Python Forum

Full Version: prefix ID Number with 0,00 make 3 digit.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Team,

Need help for converting all ID no into minimum 3 digit. pythonic way.

Check the id no, if id is single digit, add two zeros infront of it.
if id is double digit, add one zeros infront of it.
if id is >=3. no prefix required.

below is rough sketch
forcast_id = 1

if forecast_id == 1:
    forcast_id = "001"   #single digit value prefix 00 to it (0-9)
    print(forcast_id)

if forecast_id == 11:
    forcast_id = "011"   #double digit value prefix 0 to it  ( 11 - 99)
    print(forcast_id)

if forecast_id >= 100:
    forcast_id = 100 or max id
    print(forcast_id)
for i in [1, 11, 111]:
    print(f"{i:03d}")
Output:
001 011 111