r/learnpython • • Aug 09 '25

What is the purpose of the format specifier "d"?

When using the format specifier "d" within an f-string, you can only input integers. I've seen online that the utility of "d" is to convert a number into a string of decimal digits, but if the specifier can only handle ints, what is it actually doing? For everything ChatGPT says it does, I have inputted the example into Python without the "d", and all outputs remained exactly the same.

0 Upvotes

19 comments sorted by

98

u/zanfar Aug 09 '25

I've seen online that...

For everything ChatGPT says...

Stop using other people's guesses. This is a straightforward "how does Python work" question, and you should immediately be looking at the documentation, which exists for exactly this reason.

if the specifier can only handle ints, what is it actually doing?

In addition to the above, it's ensuring that an int is provided.

I have inputted the example into Python without the "d"

That's not actually testing anything relevant. You're just asking Python to format as it pleases. Very bluntly, you can't test what d does by not using d.

I'm guessing you didn't try any of the other integer literal formats, did you?

Compare what happens when you use the following integers (with d)?

  • 2147483647

  • 0o177

  • 0b100110111

  • 0xdeadbeef

  • 100_000_000_000

  • 0b_1110_0101

58

u/DreamDeckUp Aug 09 '25

upvote for docs over LLMs

41

u/C0rinthian Aug 09 '25

It is remarkable how quickly ChatGPT has made people completely helpless.

13

u/Mondoke Aug 10 '25

In my times if you wanted to avoid reading documentation you had to go to stack overflow.

9

u/a_cute_epic_axis Aug 10 '25

The chance that stack overflow had only one person posting on a topic, that hallucinated some shit into existence, is far lower. Not zero, but lower.

4

u/serverhorror Aug 10 '25

They always were, LLM usage just made it more obvious. Unfortunately, given the current state, it's for the better (at least not without knowing foundational things, and the best place for that still is the upstream documentation)

7

u/Senkyou Aug 10 '25

It has literally weaponized ignorance, interestingly. I enjoy using it to take care of time-consuming tasks, but fear using it to think for me

2

u/paradoxxr Aug 10 '25

I used it to make a spreadsheet out of a list of items and their prices and total. Was very helpful. Still had to fuck with it a bit but it was still a huge time save and that was cuz the input data was a bit ill formatted lol. That's what it's good for.

1

u/a_cute_epic_axis Aug 10 '25

Let's the rest of us collect what would have been their pay.

1

u/nog642 Aug 10 '25

What do the literal formats have to do with formatting strings? Pretty sure all that information gets lost when it's compiled to bytecode.

1

u/zanfar Aug 11 '25

becuase the purpose of the d is to specify the output format.

If you provide a decimal number, then the d appears to do nothing. If you provide a hex or octal number, then it's clear.

0

u/nog642 Aug 11 '25

No. Try using an octal or hex number without the d specifier. It still prints in decimal. The d doesn't change that.

There is no such thing as a decimal or hex or octal number in terms of the actual value stored in the variable. It doesn't store a particular representation or base, it just stores the number.

21

u/mriswithe Aug 09 '25

Reference: https://docs.python.org/3/library/string.html#format-string-syntax

d outputs it in decimal or base 10 format as opposed to

o for octal

b for binary

4

u/Kriemhilt Aug 09 '25

Since base 10 is the default, it's probably fair to say that having the d type for integers isn't very useful, except for symmetry with the other bases, and for consistency with printf and other formatters.

4

u/mriswithe Aug 09 '25

In most cases yes I would agree. Usually the purpose of something like this is for completeness/edge cases/custom objects that might return differently for use case specific things.

2

u/tidel Aug 10 '25

Default? Humans…. Laughts in computer  01111000 01100001 01111000 01100001

2

u/JorgiEagle Aug 10 '25

Unless your input isn’t in base 10, and you don’t need it to be a base 10 integer, but printing it for debugging purposes is more helpful if it is in base 10

1

u/Kriemhilt Aug 10 '25

I don't think you understand what bases are.

Bases are only ways of representing numbers. They're only relevant when parsing or formatting. 

Your values are almost certainly stored as binary, but that's not the default base for either parsing or formatting them.

1

u/QultrosSanhattan Aug 10 '25

Since note of those "read the docs" made-in-stackoverflow answers solved you question. I'll give you an LLM answer that actually works:

Alright, let me break down what d does in Python’s f-strings:

  • The d stands for decimal integer.

  • It tells Python: “Format this value as an integer in base 10.”

  • It only works with integers — if you give it a float or string, Python will throw an error.

  • Using :d is explicit — it says: “show this number as a decimal integer”.

  • Without it, Python usually defaults to decimal for integers anyway, so visually you often won’t see a difference.

  • But with d, you enforce that the value must be an integer and formatted in decimal.