I just came across this Python code, my question is about the syntax in the print statement:
class Point(object):
"""blub"""
#class variables and methods
blank = Point
blank.x = 3.0
blank.y = 4.0
print('(%g,%g)' % (blank.x,blank.y))
This print statement simply prints (3.0, 4.0), i.e. the values in blank.x and blank.y.
I don't understand the % operator in front of the (blank.x, blank.y) in the last line. What does it do and where can I find it in the documentation?
Googling this, I always end up with the modulus operator.
The %
operator in python for strings is used for something called string substitution.
String and Unicode objects have one unique built-in operation: the % operator (modulo).
This is also known as the string formatting or interpolation operator.
Given format % values (where format is a string or Unicode object), % conversion specifications in format are replaced with zero or more elements of values. The effect is similar to the using sprintf() in the C language.
If format is a Unicode object, or if any of the objects being converted using the %s conversion are Unicode objects, the result will also be a Unicode object.
'd' Signed integer decimal.
'i' Signed integer decimal.
'o' Signed octal value. (1)
'u' Obsolete type – it is identical to 'd'. (7)
'x' Signed hexadecimal (lowercase). (2)
'X' Signed hexadecimal (uppercase). (2)
'e' Floating point exponential format (lowercase). (3)
'E' Floating point exponential format (uppercase). (3)
'f' Floating point decimal format. (3)
'F' Floating point decimal format. (3)
'g' Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)
'G' Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. (4)
'c' Single character (accepts integer or single character string).
'r' String (converts any Python object using repr()). (5)
's' String (converts any Python object using str()). (6)
'%' No argument is converted, results in a '%' character in the result.
These are the values that can be substituted. To substitute, you simply follow the following syntax:
string%values #where string is a str or unicode and values is a dict or a single value
>>> print '%(language)s has %(number)03d quote types.' % \
... {"language": "Python", "number": 2}
Python has 002 quote types.
>>> print "My name is %s"%'Anshuman'
My name is Anshuman
>>> print 'I am %d years old'%14
I am 14 years old
>>> print 'I am %f years old' % 14.1
I am 14.1 years old
another example:
def greet(name):
print 'Hello, %s!'%name
A conversion specifier contains two or more characters and has the following components, which must occur in this order:
'(%g,%g)'
is the template and (blank.x,blank.y)
are the values which need to be filled in place of %g
and %g
and %
operator does just that. Its called String interpolation or formatting operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With