They are used for formatting strings. %s acts a placeholder for a string while %d acts as a placeholder for a number. Their associated values are passed in via a tuple using the % operator.

name = 'marcog'
number = 42
print '%s %d' % (name, number)

will print marcog 42. Note that name is a string (%s) and number is an integer (%d for decimal).

See https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting for details.

In Python 3 the example would be:

print('%s %d' % (name, number))
Answer from moinudin 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)
🌐
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.

🌐
W3Schools
w3schools.com › python › ref_string_format.asp
Python String format() Method
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The format() method formats the specified value(s) and insert them inside the string's placeholder.
🌐
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 - 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. There are three types of formatting in Python that pertain to strings.
🌐
Learn Python
learnpython.org › en › String_Formatting
String Formatting - Learn Python - Free Interactive Python Tutorial
Your current balance is $53.44.", no_output_msg= "Make sure you add the `%s` in the correct spaces to the `format_string` to meeet the exercise goals!") test_object('format_string') success_msg('Great work!') This site is generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science.
🌐
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 - In Python, the % operator is used for string formatting. It interprets the left argument much like a printf-style format string to be applied to the right argument, allowing you to format strings in a variety of ways. Two of the most common format specifiers used with the % operator are %s and %d.
Find elsewhere
🌐
Programiz
programiz.com › python-programming › methods › built-in › format
Python format()
It controls how the number is visually presented within a designated area. Let's look at an example. # formatting numbers without specifying width no_width = format(123, 'd') print("Without specified width:", no_width)
🌐
Programiz
programiz.com › python-programming › methods › string › format
Python String format()
The format() reads the type of arguments passed to it and formats it according to the format codes defined in the string. ... Here, Argument 0 is a string "Adam" and Argument 1 is a floating number 230.2346. Note: Argument list starts from 0 in Python.
🌐
Toppr
toppr.com › guides › python-guide › references › methods-and-functions › methods › built-in › format › python-format
Python Format(): Definition, Parameters, .Format Python
October 6, 2021 - ... # d is a type option and it specifies that the number is an integer # f is a type option that specifies that the number is a float value # b is a type option that specifies the binary format # integer format print (format (100, “d”)) ...
🌐
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.
🌐
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....
🌐
W3Schools
w3schools.com › python › python_string_formatting.asp
Python String Formatting
Before Python 3.6 we used the format() method to format strings. The format() method can still be used, but f-strings are faster and the preferred way to format strings. The next examples in this page demonstrates how to format strings with the format() method.
🌐
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
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
With this site we try to show you the most common use-cases covered by the old and new style string formatting API with practical examples. All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries. Further details about ...
🌐
Medium
medium.com › @fangya › python-format-string-s-6fdf38ce8bfb
Python Format String %s %d. Python Day 18 | by Miss Discontinuity | Medium
August 3, 2020 - print ("%s is %d cm" %("yibo", 179)) yibo is 179 cmname="Yibo" height=179print("%s is %d cm" %(name, height)) Yibo is 179 cm
🌐
Python
docs.python.org › 3 › library › string.html
Common string operations — Python 3.14.7 documentation
It is exposed as a separate function for cases where you want to pass in a predefined dictionary of arguments, rather than unpacking and repacking the dictionary as individual arguments using the *args and **kwargs syntax. vformat() does the work of breaking up the format string into character data and replacement fields.