Printf coding help

How do I make this 4 lines into 2 lines:

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

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/

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:

You’ll need to do:

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

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

1 Like

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

right on. That works.
thanks.

1 Like