Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: % operator in print() statement

Tags:

python

syntax

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.

like image 788
seb Avatar asked Dec 08 '13 06:12

seb


2 Answers

Intro

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.


Usage

'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

Examples

>>> 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

Caution!

A conversion specifier contains two or more characters and has the following components, which must occur in this order:

  • The '%' character, which marks the start of the specifier.
  • Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).
  • Conversion flags (optional), which affect the result of some conversion types.
  • Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
  • Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.
  • Length modifier (optional).
  • Conversion type.

References

  • Python docs
like image 75
Anshu Dwibhashi Avatar answered Sep 28 '22 02:09

Anshu Dwibhashi


'(%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.

like image 28
thefourtheye Avatar answered Sep 28 '22 01:09

thefourtheye