convert number to string in python

后端 未结 5 1705
你的背包
你的背包 2021-01-26 17:40

I meet a problem about converting number to string.

I want to get a string \"0100\" from str(0100), but what I got is \"64\". Does any way I can do to get a string \"010

5条回答
  •  感动是毒
    2021-01-26 17:59

    Your first issue is that the literal "0100", because it begins with a digit 0, is interpreted in octal instead of decimal. By contrast, str(100) returns "100" as expected.

    Secondly, it sounds like you want to zero-fill your numbers to a fixed width, which you can do with the zfill method on strings. For example, str(100).zfill(4) returns "0100".

提交回复
热议问题