I have been studying pickling and unpickling and a came across this - can someone please explain what it stands for?
-
1This should answer your question: [What's the difference between %s and %d in Python string formatting?](https://stackoverflow.com/questions/4288973/whats-the-difference-between-s-and-d-in-python-string-formatting) – mkrieger1 Aug 23 '21 at 19:45
-
3Curious that you learnt about `pickle` before learning about string formating – Chris_Rands Aug 23 '21 at 19:46
-
`%s` is so python 2 . . . try `f-strings` – It_is_Chris Aug 23 '21 at 19:46
2 Answers
The %s
is a placeholder for strings using the old formatting methods in python. Essentially %s
gets replaced by the string value of my_int
in your example. Here is some documentation provided by @Paul Cornelius: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting.
There are a few newer ways to do this if you want to in your own code that work a bit better, pickle likely uses the old style because they have no real reason to upgrade.
F-strings
in python 3.6+ you can use fstrings by putting an f in front of a string declaration and using {variable_name}
to access a value. like in this example:
name = "John Smith" # A dummy name
email_count = 3 # A representation of users # of new emails
current_temperature = 20.3567 # A representation of the current temperature in celsius
greeting = f"Hello, {name} The weather today is {current_temperature} degrees. You have {email_count} new emails."
print(greeting)
Which would result in printing
Hello John Smith The weather today is 20.3567 degrees. You have 3 new emails.
This is equivalent using the % method to doing:
name = "John Smith" # A dummy name
email_count = 3 # A representation of users # of new emails
current_temperature = 20.3567 # A representation of the current temperature in celsius
greeting = "Hello, %s The weather today is %03.2f degrees. You have %d new emails." % (name, current_temperature, email_count)
print(greeting)
Where %s
is replaced by a string, %03.2f
is replaced by a float rounded to the nearest 2 decimal places, and %d
is replaced by an integer. One of the main reasons this method got replaced is because F-strings are easier to read, and don't require you to know the types of everything being put into them ahead of time (just uses the __repr__()
of the object), whereas for example %d
will only work with integers, or objects that can have int(obj)
called on them.

- 1,297
- 8
- 15
-
FYI, what you say about % formatting is absolutely true, but it is still part of the language and is documented here: https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting – Paul Cornelius Aug 23 '21 at 20:23
-
@PaulCornelius Perfect, I knew it was still part of it, just couldn't find the docs because they moved them from 2-3. I will update the answer, Thanks! – Kieran Wood Aug 23 '21 at 20:37
%
means parameter passed into the string.
%s
is parameter that should be treated as string
There are some other kinds of parameters types used there like %d
for decimal integers, %f
for floating point numbers etc.

- 32,350
- 22
- 54
- 79