简体   繁体   English

python 中的 int 到 str 删除前导 0

[英]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).您实际上并不需要知道它是如何工作的,但是我为使求解器不会中断而进行的检查之一是检查通过的字符串(数独板)是否为 81 个字符(9x9 数独板)。 An example of the board would be: "000000000000000000000000000384000000000000000000000000000000000000000000000000002"板的一个例子是:“000000000000000000000000000384000000000000000000000000000000000000000000000000002”

this is a sudoku that I've wanted to try since it only has 4 numbers.这是一个我想尝试的数独,因为它只有 4 个数字。 but basically, when converting the number to a string, it removes all the '0's up until the '384'.但基本上,当将数字转换为字符串时,它会删除所有的 '0' 直到 '384'。 Does anyone know how I can stop this from happening?有谁知道我怎样才能阻止这种情况发生?

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.零的丢失不会发生在将 int 转换为字符串时,而是在您首先将字符序列解析为数字时发生。

The solution: keep the input as string until you don't need the original formatting any more.解决方案:将输入保留为字符串,直到您不再需要原始格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM