In python 3.6, the fstring or "formatted string literal" mechanism was introduced.
f"{a:02}"
is the equivalent of the .format format below, but a little bit more terse.
python 3 before 3.6 prefers a somewhat more verbose formatting system:
"{0:0=2d}".format(a)
You can take shortcuts here, the above is probably the most verbose variant. The full documentation is available here: http://docs.python.org/3/library/string.html#string-formatting
print "%02d"%a is the python 2 variant
The relevant doc link for python2 is: http://docs.python.org/2/library/string.html#format-specification-mini-language
Answer from jkerian on Stack OverflowIn python 3.6, the fstring or "formatted string literal" mechanism was introduced.
f"{a:02}"
is the equivalent of the .format format below, but a little bit more terse.
python 3 before 3.6 prefers a somewhat more verbose formatting system:
"{0:0=2d}".format(a)
You can take shortcuts here, the above is probably the most verbose variant. The full documentation is available here: http://docs.python.org/3/library/string.html#string-formatting
print "%02d"%a is the python 2 variant
The relevant doc link for python2 is: http://docs.python.org/2/library/string.html#format-specification-mini-language
a = 5
print '%02d' % a
# output: 05
The '%' operator is called string formatting operator when used with a string on the left side. '%d' is the formatting code to print out an integer number (you will get a type error if the value isn't numeric). With '%2d you can specify the length, and '%02d' can be used to set the padding character to a 0 instead of the default space.
I wrote this code:
SecToConvert = 234
MinutesGet, SecondsGet = divmod(SecToConvert, 60)
HoursGet, MinutesGet = divmod(MinutesGet,60)
print("Time is:", HoursGet, ":", MinutesGet, ":", SecondsGet)And it gives this output currently:
Time is: 0 : 3 : 54
And I need it to produce "00: 03: 54" so that it's double digits. I'm not sure how to do this currently, would anyone be able to help me to correct the code in order to get the double-digit time? Thank you!
Surprisingly, people were giving only solutions that convert to small bases (smaller than the length of the English alphabet). There was no attempt to give a solution which converts to any arbitrary base from 2 to infinity.
So here is a super simple solution:
def numberToBase(n, b):
if n == 0:
return [0]
digits = []
while n:
digits.append(int(n % b))
n //= b
return digits[::-1]
so if you need to convert some super huge number to the base 577,
numberToBase(67854 ** 15 - 102, 577), will give you a correct solution:
[4, 473, 131, 96, 431, 285, 524, 486, 28, 23, 16, 82, 292, 538, 149, 25, 41, 483, 100, 517, 131, 28, 0, 435, 197, 264, 455],
Which you can later convert to any base you want
- at some point of time you will notice that sometimes there is no built-in library function to do things that you want, so you need to write your own. If you disagree, post you own solution with a built-in function which can convert a base 10 number to base 577.
- this is due to lack of understanding what a number in some base means.
- I encourage you to think for a little bit why base in your method works only for n <= 36. Once you are done, it will be obvious why my function returns a list and has the signature it has.
If you need compatibility with ancient versions of Python, you can either use gmpy (which does include a fast, completely general int-to-string conversion function, and can be built for such ancient versions – you may need to try older releases since the recent ones have not been tested for venerable Python and GMP releases, only somewhat recent ones), or, for less speed but more convenience, use Python code – e.g., for Python 2, most simply:
import string
digs = string.digits + string.ascii_letters
def int2base(x, base):
if x < 0:
sign = -1
elif x == 0:
return digs[0]
else:
sign = 1
x *= sign
digits = []
while x:
digits.append(digs[int(x % base)])
x = int(x / base)
if sign < 0:
digits.append('-')
digits.reverse()
return ''.join(digits)
For Python 3, int(x / base) leads to incorrect results, and must be changed to x // base:
import string
digs = string.digits + string.ascii_letters
def int2base(x, base):
if x < 0:
sign = -1
elif x == 0:
return digs[0]
else:
sign = 1
x *= sign
digits = []
while x:
digits.append(digs[x % base])
x = x // base
if sign < 0:
digits.append('-')
digits.reverse()
return ''.join(digits)