| | |
1 digit number 2 to 3 digit number 002
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 21 Days Ago
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; 21 Days Ago at 12:45 am.
May 'the Google' be with you!
2
#3 21 Days Ago
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 20 Days Ago
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 |
abrupt alarm ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog cx-freeze data decimals dictionaries dictionary directory dynamic error examples exe file float format function gnu graphics gui halp heads homework http ideas import input java launcher leftmouse line linux list lists loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext sqlite statistics string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia write wxpython xlib






