OSDir


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

$s and %d in python


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.

Cheers,
Cameron Simpson <cs at cskk.id.au>