1 digit number 2 to 3 digit number 002

Thread Solved

Join Date: Oct 2009
Posts: 8
Reputation: efecto is an unknown quantity at this point 
Solved Threads: 0
efecto efecto is offline Offline
Newbie Poster

1 digit number 2 to 3 digit number 002

 
0
  #1
21 Days Ago
Hi all, I have found some examples on the net but was not able to use the technique..

here is what I would like to do.

tst = 2
numOfDigits = 3
# change this to
tst2 = 002

Any help is appreciated.
Thank you.


http://www.webdotdev.com/nvd/content.../99999999/1/2/
>>> print “z is (%6d)” % 175
z is ( 175)
>>> print “z is (%06d)” % 175
z is (000175)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #2
21 Days Ago
You can use one of the built-in string functions ...
  1. n = 2
  2. digits = 3
  3. ns = str(n).rjust(digits, '0')
  4.  
  5. print(ns) # 002
Last edited by vegaseat; 21 Days Ago at 12:45 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 900
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 210
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark
 
2
  #3
21 Days Ago
You can also use format
  1. message = "z's value is {z:0{digits}n}".format(z=2, digits=3)
  2. print(message)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 8
Reputation: efecto is an unknown quantity at this point 
Solved Threads: 0
efecto efecto is offline Offline
Newbie Poster
 
0
  #4
20 Days Ago
Thank you vegaseat and gribouillis. That's exactly what I wanted to know. I'm using vegaseat's method for now.

Cheers.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
20 Days Ago
There is an even more specific string function ....
  1. n = 2
  2. digits = 3
  3. ns = str(n).zfill(digits)
  4.  
  5. print(ns) # 002
These string functions work with Python2 and Python3.

Gribouillis, thanks for the Python3 code.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC