Python How do I turn the number 120 to 00120 in python?

  • Thread starter Thread starter vorcil
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
To format a number in Python to display leading zeros, the recommended approach is using the string formatting method. For an integer variable like xstart, you can achieve the desired output by using the format string '%05d'. For example, printing xstart as print('%05d' % xstart) will display it as 00000. After modifying xstart by adding 120 (xstart += 120), printing it again with the same format will show 00120. It's suggested to develop skills in looking up such formatting techniques for better programming proficiency.
vorcil
Messages
395
Reaction score
0

Homework Statement



I need to know how to change the format of a number in python

for example if I have a integer variable, xstart=0.
If i were to print(xstart) I want it to show 00000
and if I were to add 120 to xstart
xstart+=120
then print it
print(xstart) I want it to show 00120

Thanks.

Homework Equations





The Attempt at a Solution

 
Technology news on Phys.org
You mean something like this?

print('%05d' % xstart)

It is possible to look these things up. That would be a good skill to acquire.
 
Last edited:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top