The "%" operator is used to format a set of variables enclosed in a tuple (a fixed size list), together with a format string.

print ("%d squared is %d" % (10, 10*10))

The % operators in the string are replaced in order by the elements in the tuple.

%d is used for integers, where as %s is used for strings

Eg.

>>>name = "John"
>>>age = 23
>>>print "%s is %d years old." % (name, age)
John is 23 years old.
Answer from user3636636 on Stack Overflow
🌐
Learn Python
learnpython.org › en › String_Formatting
String Formatting - Learn Python - Free Interactive Python Tutorial
For example: # This prints out: A list: [1, 2, 3] mylist = [1,2,3] print("A list: %s" % mylist) Here are some basic argument specifiers you should know: %s - String (or any object with a string representation, like numbers) ... %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot. %x/%X - Integers in hex representation (lowercase/uppercase)
🌐
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

':10d' in f string
On this page about 2/3rds of the way down, is this:- print(f"{team:10} ==> {score:10d}") I get that :10 means ‘up to 10 chararcters long’, and the article tells me that the 'd’means digit. Are there any other characters like ‘d’? Ie something that specifies what type of character ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
4
0
January 2, 2022
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. ... That's some very basic Python, look it up in the manual or the tutorial you use. More on stackoverflow.com
🌐 stackoverflow.com
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 does {:d} do? - Stack Overflow
For the case of {:d}, which I agree is very hard to search for online, see this section ... Save this answer. ... Show activity on this post. In python you can specify the format inside the curved brackets. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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)
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
':10d' in f string - Python
January 2, 2022 - On this page about 2/3rds of the way down, is this:- print(f"{team:10} ==> {score:10d}") I get that :10 means ‘up to 10 chararcters long’, and the article tells me that the 'd’means digit. Are there any other chara…
🌐
Readthedocs
d.readthedocs.io › en › latest › examples.html
Examples — Welcome to Quick Start with D!
PyD is a library that provides seamless interoperability between the D programming language and Python. The minimal configuration file for this example is
Find elsewhere
🌐
AskPython
askpython.com › python › string › 02d-in-python
What does {:02d} mean in Python? - AskPython
July 28, 2023 - In such case, if you want to add ... As we have seen in the above example, {:02d} adds one leading zero to all the single-digit numbers so that the width of the number is equal to 2....
🌐
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.
🌐
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 of formatting using % in Python.
🌐
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.

🌐
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 - username = "Bob" print("Logged in user: %s" % username) ... 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:
🌐
Python
peps.python.org › pep-0822
PEP 822 – Dedented Multiline String (d-string) | peps.python.org
January 5, 2026 - For example, " hello" and "\thello" have no common indentation. The dedentation process removes the determined indentation from every line in the string. Lines that are longer than or equal in length to the determined indentation must start ...
🌐
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...
🌐
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
🌐
C# Corner
c-sharpcorner.com › blogs › alphabet-pattern-sharp39dsharp39-in-python
Alphabet Pattern 'D' in Python
November 2, 2015 - for Col in range(0,7): if (Col == 1 or ((Row == 0 or Row == 6) and (Col > 1 and Col < 5)) or (Col == 5 and Row != 0 and Row != 6)): str=str+"*" else: str=str+" " str=str+"\n" print(str); Output: People also reading · Membership not found · Ebook download View all · Python Overview ·
🌐
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 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.