deebah
deebah

Reputation: 3

int to str in python removes leading 0s

So right now, I'm making a sudoku solver. You don't really need to know how it works, but one of the checks I take so the solver doesn't break is to check if the string passed (The sudoku board) is 81 characters (9x9 sudoku board). An example of the board would be: "000000000000000000000000000384000000000000000000000000000000000000000000000000002"

this is a sudoku that I've wanted to try since it only has 4 numbers. but basically, when converting the number to a string, it removes all the '0's up until the '384'. Does anyone know how I can stop this from happening?

Upvotes: 0

Views: 201

Answers (1)

Amadan
Amadan

Reputation: 198476

There is no way to prevent it from happening, because that is not what is happening. Integers cannot remember leading zeroes, and something that does not exist cannot be removed. The loss of zeroes does not happen at conversion of int to string, but at the point where you parse the character sequence into a number in the first place.

The solution: keep the input as string until you don't need the original formatting any more.

Upvotes: 2

Related Questions