How to formate int in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to formate int in python?

Does anybody know how to print out an integer in two space holders for example like x=2 i want the output to look like this : x=02

21st Mar 2022, 4:15 PM
Syrine Ayedi
Syrine Ayedi - avatar
3 Answers
+ 4
# You can use f-string format options: x = 3 print(f'{x:>03}') # Another way could be using the number as string and then use the string-method zfill() print(f'{x}'.zfill(3))
21st Mar 2022, 4:21 PM
Lisa
Lisa - avatar
+ 3
an other f-string version: x = 2 print(f"x={x:02d}")
21st Mar 2022, 6:16 PM
Lothar
Lothar - avatar
+ 1
Thank you all I've used print("%02d"%x) and it worked also
21st Mar 2022, 4:29 PM
Syrine Ayedi
Syrine Ayedi - avatar