$s and %d in python

Terry Reedy tjreedy at udel.edu
Sat Jun 30 19:43:57 EDT 2018


On 6/30/2018 7:04 PM, Cameron Simpson wrote:
> On 30Jun2018 05:01, Sharan Basappa <sharan.basappa at gmail.com> wrote:
>> Is there any difference in %d and %s below. I get the same result:
>>
>> my_name = 'Zed A. Shaw'
>> my_age = 35 # not a lie
>> my_height = 74 # inches
>>
>> print "Let's talk about %s." % my_name
>> print "He's %d inches tall." % my_height
>> print "He's %s inches tall." % my_height
>>
>> Let's talk about Zed A. Shaw.
>> He's 74 inches tall.
>> He's 74 inches tall.
> 
> %d interpolates a decimal string repesentation of a numeric value > %s interpolates the default "str" representation of an arbitrary value.
> 
> The variable my_height is an int, and for an int both these things are 
> the same.
> 
> Try:
> 
>   my_height = 7.3
> 
> and see what you get.

or my_height = '74'

-- 
Terry Jan Reedy





More information about the Python-list mailing list