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)
🌐
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.
🌐
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
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.
🌐
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.
🌐
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
Find elsewhere
🌐
Python
docs.python.org β€Ί 3 β€Ί library β€Ί string.html
string β€” Common string operations
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.
🌐
PyFormat
pyformat.info
PyFormat: Using % and .format() for great good!
Since the elements are not represented by something as descriptive as a name this simple style should only be used to format a relatively small number of elements. ... With new style formatting it is possible (and in Python 2.6 even mandatory) to give placeholders an explicit positional index.
🌐
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.
🌐
Programiz
programiz.com β€Ί python-programming β€Ί methods β€Ί string β€Ί format
Python String format()
There's an easier way to format dictionaries in Python using str.format(**mapping).
🌐
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) ... for Python – so e.g. %ld is identical to %d. ... Signed integer decimal. ... Signed integer decimal. ... Signed octal value. The alternate form causes a leading zero (β€˜0’) to be inserted between left-hand padding and the formatting of the number ...
🌐
LinkedIn
linkedin.com β€Ί pulse β€Ί python-strings-format-mr-examples
Python Strings Format
May 18, 2023 - Using curly brackets, we define the placeholder: {}. In the Placeholder section below, you can learn more about the placeholders. Strings and numbers cannot be combined like this in Python Variables: When we talk about Python strings format then in Python, a string is a sequence of characters enclosed in either single quotes (β€˜ β€˜) or double quotes (” β€œ).
🌐
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
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 ... F-String was introduced in Python 3.6, and is now the preferred way of formatting strings.
🌐
Real Python
realpython.com β€Ί python-formatted-output
A Guide to Modern Python String Formatting Tools – Real Python
February 1, 2025 - You can take this quiz to test your understanding of modern tools for string formatting in Python. These tools include f-strings and the .format() method. Python has developed different string interpolation and formatting tools over the years.
🌐
YouTube
youtube.com β€Ί watch
Difference between %s and %d in Python string formatting - YouTube
%s is the python string character. Frequently used in conjunction with strings. To hold the space for string.%d is the python string character. Frequently u...
Published: March 6, 2020
🌐
Python documentation
docs.python.org β€Ί 3 β€Ί tutorial β€Ί inputoutput.html
7. Input and Output β€” Python 3.14.7 documentation
Rather than having users constantly writing and debugging code to save complicated data types to files, Python allows you to use the popular data interchange format called JSON (JavaScript Object Notation). The standard module called json can take Python data hierarchies, and convert them to string representations; this process is called serializing.