944,111 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 961
  • Python RSS
Nov 6th, 2009
0

1 digit number 2 to 3 digit number 002

Expand Post »
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.


Quote ...
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)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efecto is offline Offline
11 posts
since Oct 2009
Nov 6th, 2009
1
Re: 1 digit number 2 to 3 digit number 002
You can use one of the built-in string functions ...
Python Syntax (Toggle Plain Text)
  1. n = 2
  2. digits = 3
  3. ns = str(n).rjust(digits, '0')
  4.  
  5. print(ns) # 002
Last edited by vegaseat; Nov 6th, 2009 at 12:45 am.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2009
2
Re: 1 digit number 2 to 3 digit number 002
You can also use format
python Syntax (Toggle Plain Text)
  1. message = "z's value is {z:0{digits}n}".format(z=2, digits=3)
  2. print(message)
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,656 posts
since Jul 2008
Nov 6th, 2009
0
Re: 1 digit number 2 to 3 digit number 002
Thank you vegaseat and gribouillis. That's exactly what I wanted to know. I'm using vegaseat's method for now.

Cheers.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
efecto is offline Offline
11 posts
since Oct 2009
Nov 6th, 2009
0
Re: 1 digit number 2 to 3 digit number 002
There is an even more specific string function ....
python Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Drwaing Circles?
Next Thread in Python Forum Timeline: Ack... drowning... in... parentheses...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC