I have a string like “0001”

i am using int() and it returns 1 but i need 0001 and the type should be int, how can i do this?

When you say that you need 0001, which type of data do you need this as? In other words, what operations do you need to do with it? Take into account that by having the string `"0001"`

, you have the 0001. If you want to transform this string into something else, it must be because you would like to do other stuff with it, that perhaps the string is not suitable to do.

Also, the type `int`

is not suitable to store those zeros at the left, since equality of `int`

ignores the, for example.

I have a function that takes int and returns int,

the function itself sorts the digits in the number, and if I enter 000321 it will return 123 because int() discards leading zeros, the string type works fine but the system for checking on the school website requires the type to be int and I don’t know what to do

Is the task that you ultimately want, to sort the digits?

the task is to write two functions, one sorts the digits in a number and the other, using this function, sorts the digits in an arbitrary string with any characters and one of the tests is checking the type of the sorting function

with str

and my code

```
def Sort(x:int):
x = str(x)
x = list(x)
a = ""
for i in range(len(x)):
for j in range(i+1, len(x)):
if int(x[i])>int(x[j]):
x[i],x[j] = x[j],x[i]
return "".join(x)
def chisla(s:str):
ans = ""
allnums = []
for i in range(len(s)):
if s[i].isdigit():
allnums.append(int(s[i]))
allnums = list(map(str, allnums))
allnums = Sort("".join(allnums))
Index = 0
for i in range(len(s)):
if s[i].isdigit():
ans += str(str(allnums)[Index])
Index+=1
else:
ans+=s[i]
return ans
print(chisla("000000asasasdsd149712478"))
```

The function is currently mistyped as supporting `int`

, if leading zeros are important.

Perhaps make it take an iterable of `int`

, and pass in `[int(c) for c in str_]`

i need class int not list

I’ve used the following in cases where I needed to preserve zero-padding:

```
class PaddedInt(int):
""" Integer type that preserves zero-padding
Acts like an int in almost all ways except that string formatting
will keep the original zero-padding. Numeric format specifiers
>>> PaddedInt(1)
1
>>> p2 = PaddedInt("02")
>>> p2
02
>>> str(p2)
'02'
>>> p2 == 2
True
>>> p2 in range(3)
True
>>> f"{p2}"
'02'
>>> f"{p2:s}"
'02'
>>> f"{p2!s}"
'02'
>>> f"{p2!r}"
'02'
>>> f"{p2:d}"
'2'
>>> f"{p2:03d}"
'002'
>>> f"{p2:f}"
'2.000000'
>>> {2: "val"}.get(p2)
'val'
>>> {p2: "val"}.get(2)
'val'
Note that arithmetic will break the padding.
>>> str(p2 + 1)
'3'
"""
def __init__(self, val):
self.sval = str(val)
def __eq__(self, val):
return val == self.sval or super().__eq__(val)
def __str__(self):
return self.sval
def __repr__(self):
return self.sval
def __format__(self, format_spec):
"""Format a padded integer.
If a format spec can be used on a string, apply it to the zero-padded string.
Otherwise format as an integer.
"""
try:
return format(self.sval, format_spec)
except ValueError:
return super().__format__(format_spec)
def __hash__(self):
return super().__hash__()
```

That’s great. Might need a `super().__init__(val)`

.

But OP still has to change the type signature of their function from `int`

to `PaddedInt`

.

It’s a much better idea fix the incorrect broken design of function, instead of tip-toeing around it.

I think its to complicated for me, Is there any simpler solution?

Is there any simpler solution

Fix the broken code.

It’s not broken

A function that requires leading zeros, cannot be called with a normal `int`

that doesn’t have leading zeros.

Oh I understand I can just add 10^len() and then discard first character

I’m not sure. If I knew the number of digits required, or the number of leading zeros to prepend, (and it really had to take an `int`

) I would convert it back to a `str`

and call `.zfill`

:

If something faster was required, it’s not an `int`

sub class, and requires messing around with bytes to convert to decimal digits with leading zeros, but it would be natural to pick a fixed-width integer datatype (with sufficient width), e.g.:

https://numpy.org/devdocs/reference/arrays.scalars.html#numpy.uint64

Try this:

```
DIGITS = 4 # Displayed length
num = 75 # Misc test number
def pad_nums(num, num_length):
num_str = str(num)
num_len = len(num_str)
# num_list = list(num_str)
zeros = num_length - num_len
adder = 0
result = []
while adder < zeros:
result.append(str(0))
adder += 1
result.append(num_str)
zero_padded_num = ''.join(sorted(result))
return zero_padded_num
print(pad_nums(num, DIGITS))
# If you want to make use of the result for some mathematical operation, just convert to int just prior to the operation
```

… generalized to a list of numbers:

```
DIGITS = 4 # Displayed length
num_list = [2, 53, 121, 8567, 5]
def pad_nums(num_length, nums):
padded_num_list = []
for num in nums:
num_str = str(num)
num_len = len(num_str)
# num_list = list(num_str)
zeros = num_length - num_len
adder = 0
result = []
while adder < zeros:
result.append(str(0))
adder += 1
result.append(num_str)
zero_padded_num = ''.join(sorted(result))
padded_num_list.append(zero_padded_num)
return padded_num_list
print(pad_nums(DIGITS, num_list))
```

You can’t sort an int if it has leading zeroes, as an int because ‘0001’ is a string. Sort it as a string first, then at the end of the program display the number as an int by converting to int. In other languages to remove the leading zeroes we add zero.

Here’s the general idea:

```
mystr = '0001'
print(int(mystr))
```

You can’t do this because integers don’t *have* leading zeros. The entire point of “integer” is that it represents a number, something you can do math with. We don’t care about the leading zeros there. If you start with `0001`

, it *means the same thing as* `1`

, and we forget the zeros when we do the math. In math class, you would never say “the answer is no thousands, no hundreds, no tens and one”.

Anything that you enter is a string. If you want something else then it has to be converted. But you can’t convert `000321`

and `321`

into different integers, because there don’t exist different integers to represent them.

The number has the digits that it has. If you convert those strings to integer, then there are no leading zeros to sort (when the function converts it back to a string to get the digits).

Yes. The trick is, you have to understand that the zeros will disappear, and account for that somehow.

(Also: after you get that result back, you don’t have to process the characters again to check for digits.)

(Also: it looks like the test failure is telling you a different problem. I think they want your integer-digit-sort function to *return* an integer, rather than a string. Of course, in this case, it won’t only be *leading* zeros that disappear. Or rather, after sorting, any other zeros would become “leading”, and also disappear. Maybe it would help, to check how many of them there are, first?)