In python you can specify the format inside the curved brackets.

You can do things such as

>>> '{:b}'.format(2)
'10'

In your case, d prints as decimal integer.

You can find all the doc here

https://docs.python.org/2/library/string.html#format-specification-mini-language

Answer from BlueSheepToken on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › what does %d and %s do in python and why do we use it?
r/learnpython on Reddit: What does %d and %s do in Python and why do we use it?
August 18, 2024 -

Hello, so I am starting to learn python on my own learnpython.org and I am currently on the string formatting section and it starts talking about %d and $s. I kinda understand what it does but not really. However my main question is why do we use %s and %d. One example I have seen was

name = 'Geek

print ("Hey, %s,!" % name)

This printed out "Hey, Geek!". But why do we use it when we can also just do

print ("Hey", name)

As I am typing this out I kind of see the use but still any advice would be helpful

Edit: Thanks for all the replies they really helped!! I will be sending in thousands of more questions as I continue to learn.

Top answer
1 of 7
103
Python f string formatting has largely replaced the need for either the % formatting or the "{}" .format() approach, I recommend you learn that!
2 of 7
23
There are multiple ways to insert a variable into a string and format it. The mod % operator allows insertion of variables from a tuple. base = 'Hello %s%s My favourite number is %d.' variables = ('World', '!', 4) base % variables This returns: 'Hello World! My favourite number is 4.' The s indicates that a string is to be inserted and the d indicates that a decimal integer is to be inserted. The course you are following must be pretty old as there have been newer ways to format strings which are recommended over use fo the % operator. These revolve around the format method: base = 'Hello {0}{1} My favourite number is {2}' variables = ('World', '!', 4) base.format(*variables) This returns: 'Hello World! My favourite number is 4.' Most Python IDEs will apply syntax highlighting (Reddit doesn't unfortunately) which makes it clear that a variable is posiitonally being inserted into the braces. In this case the numeric values relate to the index of the tuple. It is more common to use named parameters: base = 'Hello {who}{exclaim} My favourite number is {num}.' variables = (who='World', exclaim='!', num=4) base.format(*variables) This returns: 'Hello World! My favourite number is 4.' The * means you are unpacking the tuple removing the parenthesis, essentially supplying who='World', exclaim='!', num=4 to the function instead of (who='World', exclaim='!', num=4) Now when you have the variables defined as: who = 'World' exclaim = '!' num = 4 base = 'Hello {who}{exclaim} My favourite number is {num}.' variables = dict(who=who, exclaim=exclaim, num=num) base.format(**variables) The ** means you are unpacking the dictionary, removing the dict() enclosing dict(who=who, exclaim=exclaim, num=num) giving who=who, exclaim=exclaim, num=num. Notice the last three lines can be combined: 'Hello {who}{exclaim} My favourite number is {num}.'.format(who=who, exclaim=exclaim, num=num) However it becomes tedious to mention each variable 3 times... so this can be abbreviated using the prefix f: f'Hello {who}{exclaim} My favourite number is {num}.' In the {} you can insert the variable, but you can also use a colon to include a format specifier. For example: f'Hello {who:s}{exclaim:s} My favourite number is {num:d}.' You can set the width of each variable: f'Hello {who:10s}{exclaim:10s} My favourite number is {num:10d}.' 'Hello World ! My favourite number is 4.' If you want to see the leading zeros for the number you can add the 0 prefix: f'Hello {who:10s}{exclaim:10s} My favourite number is {num:010d}.' 'Hello World ! My favourite number is 0000000004.' There is a string format specification minilanguage. For more details see the Python Documentation: string Miniformat Language . Most of the format specification is for inserting numbers into a string specifically a floating point format. It is easier to see whats happening by looking at the examples at the bottom of the page. Using f will five the fixed format for example 0.1234 (this has a width of 6 characters, a precision of 4 characters after the decimal point so can be 6.4f). Using e will give the exponent format 1.234e-1 for example (this has a width of 8 characters and a precision of 3 characters so can be 6.3e). I cover some common use cases in this video YouTube Spyder IDE: Numeric Values (20 mins in)
🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-s-and-d-in-python-string
Difference between %s and %d in Python string - GeeksforGeeks
July 23, 2025 - The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value. The following code illustrates the usage of the %s symbol : ... # declaring a string variable name = "Geek" # append a string within a string print("Hey, %s!" % name)
Discussions

What is the purpose of the format specifier "d"?
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 More on reddit.com
🌐 r/learnpython
19
0
August 9, 2025
python - What's the difference between %s and %d in string formatting? - Stack Overflow
I don't understand what %s and %d do and how they work. More on stackoverflow.com
🌐 stackoverflow.com
What does {:d} mean? Strings Python 3.4.3 - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... The 2026 Annual Developer Survey is live— take the Survey today! pythonjavascriptc#reactjsjavaandroidhtmlflutterc++node.jstypescriptcssrphpangularnext.jsspring-bootmachine-learningsqlexceliosazuredocker More on stackoverflow.com
🌐 stackoverflow.com
How do we use the Python string formatters %d and %f?
Question How do we use the Python string formatters %d and %f? Answer In Python, string formatters are essentially placeholders that let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers. More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
2
August 31, 2018
People also ask

Why does %d raise a TypeError?
The supplied value may not be an integer-compatible value, or the formatting operator may receive the wrong number or shape of arguments.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python %d formatting: integer placeholders and modern alternatives
Python %d Formatting: Integer Placeholders and Modern Alternatives
Should I use %d or an f-string?
Use f-strings for new code when the project supports them; keep %d when maintaining legacy formatting or matching an existing interface.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python %d formatting: integer placeholders and modern alternatives
Python %d Formatting: Integer Placeholders and Modern Alternatives
🌐
AskPython
askpython.com › python › string › 02d-in-python
What does {:02d} mean in Python? - AskPython
July 28, 2023 - To know more about Python Strings, check out this article. In this article, we will brush through different ways of string formatting and how {:02d} is related and used in string formatting.
🌐
Stack Abuse
stackabuse.com › the-difference-between-s-and-d-in-python-string-formatting
The Difference Between %s and %d in Python String Formatting
July 2, 2023 - The %s specifier converts the object ... to their string representation: ... Hello, Bob! On the other hand, the %d format specifier is a placeholder for a decimal integer....
Find elsewhere
🌐
Quora
quora.com › What-are-the-differences-among-s-r-and-d-in-Python
What are the differences among %s, %r and %d in Python? - Quora
Answer (1 of 5): From the python documentation, %s : String (converts any Python object using str()). %r : String (converts any Python object using repr()). %d : Signed integer decimal. For the technical differences between %s and %r check out this answer on stackover flow : Page on stackoverfl...
🌐
Udacity
udacity.com › blog › 2020 › 11 › python-string-format-whats-the-difference-between-s-and-d.html
Python String Format: What's the Difference Between %s and %d? | Udacity
October 24, 2024 - These components allow for finer control over the display of certain conversion types, but remain optional. Only the modulo symbol and the conversion type are required. While elaborations on many different conversion types may be found in this Real Python tutorial, we’ll focus exclusively on two popular conversion types: %s and %d.
🌐
Python Pool
pythonpool.com › home › tutorials › python %d formatting: integer placeholders and modern alternatives
Python %d Formatting: Integer Placeholders and Modern Alternatives
July 13, 2026 - When the string has more than one placeholder, pass a tuple of values in the same order. apples = 5 oranges = 3 print("Apples: %d, oranges: %d" % (apples, oranges)) ... You can add a minimum width between % and d. Add 0 before the width for zero padding. number = 42 print("]" % number) print("d" % number) ... When you pass a float to %d, Python converts it to an integer-style value by truncating toward zero.
🌐
Reddit
reddit.com › r/learnpython › what is the purpose of the format specifier "d"?
r/learnpython on Reddit: What is the purpose of the format specifier "d"?
August 9, 2025 -

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.

🌐
Learn Python
learnpython.org › en › String_Formatting
String Formatting - Learn Python - Free Interactive Python Tutorial
You can save 25% off your Datacamp annual subscription with the code LEARNPYTHON23ALE25 - Click here to redeem your discount · Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d".
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
How do we use the Python string formatters %d and %f? - Python FAQ - Codecademy Forums
August 31, 2018 - Question How do we use the Python ... in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers....
🌐
Facebook
facebook.com › groups › programming1group › posts › 1864956527171616
Please what does %d mean in python
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment
🌐
Quora
quora.com › In-Python-what-s-the-difference-between-d-and-I-formatting
In Python what’s the difference between %d and %I formatting? - Quora
Answer (1 of 2): According to all the documentation.I can find there is no difference between them (they both format signed Decimal integers). Every official document I can find lists the %d (or whatever) style of formatting obsolete and likely to be removed at some point. I would suggest learn ...
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › formatting.html
% (String Formatting Operator) — Python Reference (The Right Way) 0.1 documentation
A length modifier (h, l, or L) may be present, but is ignored as it is not necessary for Python – so e.g. %ld is identical to %d. ... Signed integer decimal.
🌐
Python
peps.python.org › pep-0822
PEP 822 – Dedented Multiline String (d-string) | peps.python.org
January 5, 2026 - It allows specifying the amount of indentation to be removed more easily. It can dedent continuation lines. It is considered that using triple backticks for dedented multiline strings could be an alternative syntax. This notation is familiar to us from Markdown. While there were past concerns about certain keyboard layouts, nowadays many people are accustomed to typing this notation. However, this notation conflicts when embedding Python ...