🌐
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 %d where an integer should appear in a string, then provide the value after the % operator. ... 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" ...
🌐
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)
What does "%" mean in Python? Jul 3, 2024
r/learnpython
2y ago
Hard time understanding string formatting in Python Jul 24, 2018
r/learnprogramming
8y ago
Why should I use %d and %s to format Strings? Sep 14, 2021
r/learnpython
5y ago
What is format string Feb 8, 2026
r/learnpython
7mo ago
More results from reddit.com
Discussions

python - What's the difference between %s and %d in string formatting? - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... pythonjavascriptc#reactjsjavaandroidhtmlflutterc++node.jstypescriptcssrphpangularnext.jsspring-bootmachine-learningsqlexceliosazuredocker ... Next You’ll be prompted to create an account to view your personalized homepage. Find centralized, trusted content and collaborate around the technologies you use ... More on stackoverflow.com
🌐 stackoverflow.com
%d within print statement python - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... pythonjavascriptc#reactjsjavaandroidhtmlflutterc++node.jstypescriptcssrphpangularnext.jsspring-bootmachine-learningsqlexceliosazuredocker ... Next You’ll be prompted to create an account to view your personalized homepage. Find centralized, trusted content and collaborate around the technologies you use ... More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between %i and %d in Python? - Stack Overflow
Releases Keep up-to-date on features ... Stack Internal. ... The 2026 Annual Developer Survey is live— take the Survey today! pythonjavascriptc#reactjsjavaandroidhtmlflutterc++node.jstypescriptcssrphpangularnext.jsspring-bootmachine-learningsqlexceliosazuredocker ... Next You’ll be prompted to create an account to view your personalized homepage. Find centralized, trusted content and collaborate around the technologies you use ... More on stackoverflow.com
🌐 stackoverflow.com
%i insteand of %d in formatting
For the formatting, why shouldn’t we have %i instead of %d so that we’ll get a more suggestive indication for the fact that we convert any numeric value into an integer? More on discuss.python.org
🌐 discuss.python.org
5
0
November 15, 2023
People also ask

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
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
🌐
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 ... let us pass in different values into some formatted string. The %d formatter is used to input decimal values, or whole numbers....
🌐
YouTube
youtube.com › pymoondra
Python Tutorials - "%s %d" % ("string_formatting_in_Python", 3) - YouTube
This video is the first of a series of string formatting in Python, specifically Python 3. We will start with C-style string formatting, which is the most di...
Published: August 29, 2019
Views: 4K
🌐
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.
🌐
Delft Stack
delftstack.com › home › howto › python › python string formatting
Difference Between %s and %d in Python String Formatting | Delft Stack
October 10, 2023 - The %d operator is used as a formatting string in Python. It is a placeholder for an integer. The value associated with %d is provided in a tuple using % or modulo operator. It is necessary to maintain the order of the values to be printed.
🌐
Learn Python
learnpython.org › en › String_Formatting
String Formatting - Learn Python - Free Interactive Python Tutorial
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". Let's say you have a variable called "name" with your user name in it, and you would then like to print(out a greeting to that user.)
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › string-formatting-in-python-using
Python Modulo String Formatting - GeeksforGeeks
March 12, 2024 - In Python, a string of required formatting can be achieved by different methods. Some of them are; 1) Using % 2) Using {} 3) Using Template Strings In this article the formatting using % is discussed. The formatting using % is similar to that of 'printf' in C programming language. %d - integer %f - float %s - string %x - hexadecimal %o - octal The below example describes the use ...
🌐
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 - The %d and %s values here work as keywords to indicate where the variables after the % operators need to be placed. We’ll now cover how to use argument specifiers, such as %s and %d from the example above. While we’ll specifically focus on converting strings and numbers, you’ll need to read the official Python guidelines on formatting to get more in-depth information on string modulo operators and conversion types.
🌐
LinkedIn
linkedin.com › pulse › python-strings-format-mr-examples
Python Strings Format
May 18, 2023 - It allows you to insert integer values into a string when it comes to Python strings format. Here’s an example that demonstrates the usage of %d in string formatting: age = 25 message = “I am %d years old.” % age print(message) ...
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › str › formatting.html
% (String Formatting Operator) — Python Reference (The Right Way) 0.1 documentation
When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the ‘%’ character. The mapping key selects the value to be formatted from the mapping. For example: >>> print '%(language)s has %(number)03d quote types.' % \ ... {"language": "Python", "number": 2} Python has 002 quote types.
🌐
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....
🌐
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...
🌐
TutorialsPoint
tutorialspoint.com › what-does-do-to-strings-in-python
What does % do to strings in Python?
March 6, 2025 - In the following example, we are going to insert strings into a string using %s placeholder. name = "Point" message = "Tutorials, %s!" % name print(message) ... Tutorials, Point! When we use '%d', Python expects an integer value. If you provide a non-integer value, Python will attempt to convert ...
🌐
Edureka Community
edureka.co › home › community › categories › python › what s the difference between s and d in python...
What s the difference between s and d in Python string formatting | Edureka Community
February 3, 2022 - What s the difference between s and d in Python... Reading different format files from s3 having decoding issues using boto3 May 17, 2024 · What is the algorithm to find the sum of prime numbers in the input in python Feb 22, 2024
🌐
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
🌐
Python.org
discuss.python.org › python help
%i insteand of %d in formatting - Python Help - Discussions on Python.org
November 15, 2023 - For the formatting, why shouldn’t we have %i instead of %d so that we’ll get a more suggestive indication for the fact that we convert any numeric value into an integer?