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
🌐
Python documentation
docs.python.org › 3 › tutorial › introduction.html
3. An Informal Introduction to Python — Python 3.14.7 documentation
Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.) The interpreter acts as a simple calculator: you can type an expression into it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / can be used to perform arithmetic; parentheses (()) can be used for grouping. For example: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division ...
🌐
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)
Discussions

d within print statement python
I encountered this in a function of a program and I don't understand how the %d can work inside a closed quote print statement. print "%d squared is %d." % (n, squared) The output when the argumen... More on stackoverflow.com
🌐 stackoverflow.com
python - differences between "d = dict()" and "d = {}" - Stack Overflow
You can't have a diploid punctuation character for every complex type! dict list and tuple are the core types that are supported. But I think I feel that dict() vs braces is somehow more Pythonic because it minimises language elements, so I empathise, but given these are the 3 most common ... More on stackoverflow.com
🌐 stackoverflow.com
python - What's the difference between %s and %d in string formatting? - Stack Overflow
This could be achieved with a "+" most of the time. To gain a deeper understanding to your question, you may want to check {} / .format() as well. Here is one example: Python string formatting: % vs. More on stackoverflow.com
🌐 stackoverflow.com
What does {:d} mean? Strings Python 3.4.3 - Stack Overflow
The 2026 Annual Developer Survey is live— take the Survey today! pythonjavascriptc#reactjsjavaandroidhtmlflutterc++node.jstypescriptcssrphpangularnext.jsspring-bootmachine-learningsqlexceliosazuredocker More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
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 %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified.
🌐
Readthedocs
d.readthedocs.io › en › latest › examples.html
Examples — Welcome to Quick Start with D!
+/ alias toNumpyArray = d_to_python_numpy_ndarray; /++ A static constructor is a function that performs initializations of thread local data before the `main()` function gets control for the main thread. Shared static constructors are executed before any static constructors, and are intended for initializing any shared global data.
Find elsewhere
🌐
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 - String formatting involves taking a set of variables and, using an expression, formatting them into a provided string that contains placeholders for those variables. Python uses two different styles of string formatting: the older Python 2 style that’s based on the modulo operator (%), and the newer Python 3 style that uses curly braces and colons.
🌐
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 - Use Python %d formatting for integers, width, zero padding, multiple values, and percent signs, then compare it with f-strings.
🌐
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 - Even if your data isn't a string, you can still use %s. Python will automatically convert the data to its string representation. For example, if you want to print a list as part of a string:
🌐
Bit Bashing
bitbashing.io › 2015 › 01 › 26 › d-is-like-native-python.html
D is like native Python - Bit Bashing
To make finding third-party libraries easier, D has a package manager known as DUB. Languages like Python and Ruby sacrifice speed at the altar of convenience. These scripting languages are outperformed by native and more traditional JITted languages such as Java by an order of magnitude or two, but people use them because they are so damn good.
🌐
AskPython
askpython.com › python › examples › cohens-d-python
What is Cohen's D in Python? - AskPython
April 10, 2025 - In this section, we will implement this measure in Python. We will initialize two arrays as inputs for two different groups and calculate the mean and standard deviations using the numpy library. Then by calculating the pooled standard deviation, we will calculate the Cohen’s D.
🌐
Learn Python
learnpython.org › en › String_Formatting
String Formatting - Learn Python - Free Interactive Python Tutorial
Good news! 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.
🌐
Readthedocs
d.readthedocs.io › en › stable › examples.html
Examples — Welcome to Quick Start with D! - Read the Docs
+/ alias toNumpyArray = d_to_python_numpy_ndarray; /++ A static constructor is a function that performs initializations of thread local data before the `main()` function gets control for the main thread. Shared static constructors are executed before any static constructors, and are intended for initializing any shared global data.
🌐
Readthedocs
pyd.readthedocs.io › en › latest › classes.html
Exposing D classes to python — PyD 1.0 documentation
When "", determine mode based on availability of getter and setter forms. ... only one overload is permitted per operator; however OpBinary and OpBinaryRight may “share” an operator. PyD only supports opSlice, opSliceAssign if both of their two indices are implicitly convertable to Py_ssize_t. This is a limitation of the Python/C API.
🌐
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...
🌐
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 ...