| | |
1 digit number 2 to 3 digit number 002
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 8
Reputation:
Solved Threads: 0
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.
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)
1
#2 Nov 6th, 2009
You can use one of the built-in string functions ...
Python Syntax (Toggle Plain Text)
n = 2 digits = 3 ns = str(n).rjust(digits, '0') print(ns) # 002
Last edited by vegaseat; Nov 6th, 2009 at 12:45 am.
May 'the Google' be with you!
2
#3 Nov 6th, 2009
You can also use format
python Syntax (Toggle Plain Text)
message = "z's value is {z:0{digits}n}".format(z=2, digits=3) print(message)
0
#5 Nov 6th, 2009
There is an even more specific string function ....
These string functions work with Python2 and Python3.
Gribouillis, thanks for the Python3 code.
python Syntax (Toggle Plain Text)
n = 2 digits = 3 ns = str(n).zfill(digits) print(ns) # 002
Gribouillis, thanks for the Python3 code.
May 'the Google' be with you!
![]() |
Similar Threads
- how to find number of integers in a number (C++)
- generating random number of eight digit (Perl)
- count number of ones and zeroes in a number (C)
- number of times each number in array occurs? (C)
- Finding the number of digits in a number (C++)
- Number of times a number appears in an array (C++)
Other Threads in the Python Forum
- Previous Thread: Drwaing Circles?
- Next Thread: Ack... drowning... in... parentheses...
| Thread Tools | Search this Thread |
Tag cloud for Python
abrupt ansi anti approximation assignment avogadro backend basic beginner binary bluetooth calculator character code customdialog decimals dictionaries dictionary drive dynamic examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects py2exe pygame pyqt python random recursion recursive refresh schedule scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






