| | |
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 19 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; 19 Days Ago at 12:45 am.
May 'the Google' be with you!
2
#3 19 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 18 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 ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character cmd converter countpasswordentry curved customdialog dan08 decimals dictionaries dictionary dynamic exe file float format function gnu graphics heads homework http ideas import inches input java leftmouse library line lines linux list lists loop module mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar py2exe pygame python random recursion redirect scrolledtext software sqlite statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






