Printf coding help


#1

How do I make this 4 lines into 2 lines:

  printf("%3d", 100);
  printf("\n");
  printf("%05d", 100);
  printf("\n");

#2

I don’t know C but I think you want this:

print("%d" % 100)
print("%d" % 100)

Output:

100
100

See here for more information about formatting: https://pyformat.info/


#3

Actually I wasn’t right in the above example:

I researched a bit and learned some new cool thing about formatting. Didn’t know that I can format a string that way.

Here is the right example:

# Python 2 Style
print("%5d" % 100)
print("%05d" % 100)

# Python 3 Style
print("{0:5d}".format(100))
print("{0:05d}".format(100))

# Python 3.6 Style
print(f"{100:5d}")
print(f"{100:05d}")

Output:

  100
00100
  100
00100
  100
00100

Thank you @meeee you make me learn some new cool thing :wink:


#4

You’ll need to do:

printf("%3d\n", 100);
printf("%05d\n", 100);

#5

Haha, cool thing and I did some crazy stuff but it’s that simple :joy:


#6

lol, yes, well, you tried to do it in Python.


#7

right on. That works.
thanks.