They are used for formatting strings.
%s
acts a placeholder for a string while %d
acts as a placeholder for a number. Their associated values are passed in via a tuple using the %
operator.name = 'marcog'
number = 42
print '%s %d' % (name, number)
will print
marcog 42
. Note that name is a string (%s) and number is an integer (%d for decimal).
In Python 3 the example would be:
print('%s %d' % (name, number))
example code
a="the color of my eyes is "+mystr
print(a)
print(" mystr value is : %s" % mystr)
name = 'marcog'
number = 42
print ('%s %d' % (name, number))
name = 'Ambiorix'
number = 36
print ( 'mystring y myinterger values son %s %d' % (name, number))
No hay comentarios:
Publicar un comentario