-1

I am converting a string into integer using int function and it is working fine but i want to keep save zero digit that are at the start of the string.

string_value = '0123'
print(int(string_value))
result is 123

How can i format output 0123 as in integer type value not in string.

Ahsan aslam
  • 1,149
  • 2
  • 16
  • 35
  • Possible duplicate of [Nicest way to pad zeroes to string](https://stackoverflow.com/questions/339007/nicest-way-to-pad-zeroes-to-string) – Austin May 09 '18 at 07:55
  • you want to result be `0123` and have an `int` type? it is not possible – Azat Ibrakov May 09 '18 at 08:31

3 Answers3

2

You can't, but if you want to put 0's (zero padding) at the beginning of your number, this is the way to do it.

"{:04}".format(123)
# '0123'

"{:05}".format(123)
# '00123'
BcK
  • 2,548
  • 1
  • 13
  • 27
1

Like every one said you can try above answers or the following :

string_value = '0123'
int_no = int(string_value)

print("%04d" % int_no)

print(string_value.zfill(4))

Both will give same answer

Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33
0

Impossible, you cannot get an integer value of 0123.

You should change your mind, you do not actually need 0123 in integer, but you need to keep zero when displaying it. So the question should change to how to format output.

Sraw
  • 18,892
  • 11
  • 54
  • 87