What is the difference between% s and% d in formatting Python strings?

I don’t understand what %s and %d and how they work.

+103
python string-formatting
Nov 26 '10 at 22:35
source share
11 answers

They are used to format strings. %s acts as a placeholder for a string, while %d acts as a placeholder for a number. Their associated values ​​are passed through the tuple using the % operator.

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

prints marcog 42 . Note that the name is a string (% s), and the 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, an example would be:

 print('%s %d' % (name, number)) 
+154
Nov 26 '10 at
source share

%s used as a placeholder for string values ​​that you want to insert into a formatted string.

%d used as a placeholder for numeric or decimal values.

For example (for python 3)

 print ('%s is %d years old' % ('Joe', 42)) 

It will be a day off

 Joe is 42 years old 
+25
Nov 26 '10 at 22:37
source share

from python 3 dock

%d for decimal integer

%s for a common string or object, and in the case of an object, it will be converted to a string

Consider the following code

 name ='giacomo' number = 4.3 print('%s %s %d %f %g' % (name, number, number, number, number)) 

the output will be

giacomo 4.3 4 4.300000 4.3

as you can see, %d will be truncated to an integer, %s will support formatting, %f will print as a floating point number, and %g used for the total number

obviously

 print('%d' % (name)) 

throws an exception; you cannot convert a string to a number

+18
Feb 07 '18 at 9:43
source share

These are placeholders:

For example: 'Hi %s I have %d donuts' %('Alice', 42)

This line of code will replace% s with Alice (str) and% d with 42.

Exit: 'Hi Alice I have 42 donuts'

This can be achieved with a "+" in most cases. To get a deeper understanding of your question, you can also check {} /. Format (). Here is one example: Python string formatting:% vs..format

also see here the python google @ 40 'video tutorial, there are some explanations in it https://www.youtube.com/watch?v=tKTZoB2Vjuk

+11
Jul 07 '15 at 21:33
source share

%d and %s are placeholders, they work as a replaceable variable. For example, if you create 2 variables

 variable_one = "Stackoverflow" variable_two = 45 

You can assign these variables to a sentence in a string using a tuple of variables.

 variable_3 = "I was searching for an answer in %s and found more than %d answers to my question" 

Note that %s works for String, and %d works for numeric or decimal variables.

if you type variable_3 it will look like this

 print(variable_3 % (variable_one, variable_two)) 

I searched the answer in StackOverflow and found over 45 answers to my question.

+9
Feb 26 '16 at 14:23
source share

The string formatting commands %d and %s are used to format strings. %d for numbers, and %s for strings.

For example:

 print("%s" % "hi") 

as well as

 print("%d" % 34.6) 

To pass a few arguments:

print("%s %s %s%d" % ("hi", "there", "user", 123456)) will return hi there user123456

+9
Jun 19 '16 at 21:17
source share

They are format specifiers. They are used when you want to include the value of your Python expressions in strings, with specific formatting.

See Immersion in Python for a relatively detailed introduction.

+7
Nov 26 '10 at 22:37
source share

If you want to avoid% s or% d, then ..

 name = 'marcog' number = 42 print ('my name is',name,'and my age is:', number) 

Output:

 my name is marcog and my name is 42 
+2
03 Feb '16 at 14:35
source share

Speaking of which ...
Python3.6 comes with f-strings which makes formatting much easier!
Now, if your version of Python is greater than 3.6, you can format the strings using the following methods available:

 name = "python" print ("i code with %s" %name) # with help of older method print ("i code with {0}".format(name)) # with help of format print (f"i code with {name}") # with help of f-strings 
+1
Mar 09 '18 at 8:02
source share

These are all informative answers, but none of them fully understood the difference between %s and %d .

%s tells the formatter to call the str() function for the argument, and since by definition we are casting a string to a value, %s essentially just executes str(arg) .

%d on the other hand, calls int() for the argument before calling str() , for example, str(int(arg)) , this will call both int and str .

For example, I can convert a hexadecimal value to decimal,

 >>> '%d' % 0x15 '21' 

or cut the float.

 >>> '%d' % 34.5 '34' 

But the operation will throw an exception if the argument is not a number.

 >>> '%d' % 'thirteen' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: %d format: a number is required, not str 

Thus, if the goal is simply to call str(arg) , then %s enough, but if you need additional formatting (for example, decimal floating-point formatting) or another cast, then other format characters are needed.

In f-string notation, when you are not using a formatter, str used by default.

 >>> a = 'some_string' >>> f'{a:s}' 'some_string' >>> a = 'some_string' >>> f'{a}' 'some_string' 

The same is true for string.format ; the default is str .

 >>> '{}'.format(a) 'some_string' >>> '{!s}'.format(a) 'some_string' 
+1
May 30 '19 at 16:23
source share

According to the latest standards, it should be so.

 print("My name is {!s} and my number is{:d}".format("Agnel Vishal",100)) 

Check Python3.6 docs and sample program

0
Oct 13 '18 at 14:32
source share



All Articles