Python Forum
prefix ID Number with 0,00 make 3 digit.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
prefix ID Number with 0,00 make 3 digit.
#1
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)
Reply
#2
for i in [1, 11, 111]:
    print(f"{i:03d}")
Output:
001 011 111
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] a 4-digit display vr34 2 328 Apr-12-2024, 05:38 PM
Last Post: vr34
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 364 Jan-27-2024, 05:23 PM
Last Post: Winfried
  if a string has a digit - print tester_V 2 2,155 Jan-16-2021, 04:48 AM
Last Post: tester_V
  Is this right code for 3 digit pin ? fatjuicypython 1 1,824 Aug-21-2020, 11:15 PM
Last Post: bowlofred
  Make an array of string number in a List polantas 5 3,123 May-27-2020, 07:18 AM
Last Post: buran
  how do I make a number only print once but the number keeps on decreasing? syafiq14 5 2,988 Jan-03-2020, 10:34 AM
Last Post: perfringo
  how to make a program with a certain number of multiples? syafiq14 3 2,792 Jan-01-2020, 02:39 PM
Last Post: syafiq14
  prefix to infix rontoto 4 3,971 Dec-08-2019, 12:32 PM
Last Post: rontoto
  String add prefix & suffix nahom 3 3,180 Sep-30-2019, 11:09 PM
Last Post: Gribouillis
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,565 Aug-12-2019, 01:31 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020