Home > OS >  Converting the integer 0000 into a string
Converting the integer 0000 into a string

Time:10-15

For example, I have the code:

number = 0000
a = str(number)
print(a)

Here, I get the output as 0, instead of 0000, but at the same time, when i take the number as 11111 or so on, this problem doesn't exist. how do i fix this

CodePudding user response:

Python treats 0000 and 0 as the same number.

print(0000 == 0)
=> True

You can't fix this. If you need to represent "0000" you'll have to do it as a string not a number.

CodePudding user response:

Generally in math, left zeros padding are not taken into account for integers so 001 means 1 (001 in Python is SyntaxError though). But you can create strings with left zero padding.

Two options: 1-format string 2-zfill

number = 0
print(f'{number:>04}')

and

number = 0
print(str(number).zfill(4))

CodePudding user response:

when you set the number , 0000 it just means 0 but you can put some 0 in the string varible first and fix it.

  • Related