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 OverflowHello, 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.
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 OverflowPython copied the C formatting instructions.
For output, %i and %d are the exact same thing, both in Python and in C.
The difference lies in what these do when you use them to parse input, in C by using the scanf() function. See Difference between format specifiers %i and %d in printf.
Python doesn't have a scanf equivalent, but the Python string formatting operations retained the two options to remain compatible with C.
The new str.format() and format() format specification mini-language dropped support for i and stuck with d only.
No difference.
And here's the proof.
The reason why there are two is that, %i is just an alternative to %d ,if you want to look at it at a high level (from python point of view).
Here's what python.org has to say about %i: Signed integer decimal.
And %d: Signed integer decimal.
%d stands for decimal and %i for integer.
but both are same, you can use both.