Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (4k points)

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.

1 Answer

0 votes
by (8.7k points)

Let's Fragment the python code into parts and try to understand:

class Goal(object):

"""post""" 

set = Goal

Set.p = 8.0

Set.q = 3.0

print('(%g,%g)' % (blank.x,blank.y))

 

·         (%g,%g) is referring to the  template in  python

·         (Set.p,Set.q) refers to the value that is needed to be printed

·          % operator is referred to as the formatting operator or string interpolation.

 

Let’s understand how it works

 Format  % values format can be a string or Unicode object), and  %  during conversion matter in the format are replaced with elements of values.

using sprintf() in the C language mimic this effect.

 

Related questions

0 votes
1 answer
asked Feb 19, 2021 in Java by Harsh (1.5k points)
0 votes
2 answers

Browse Categories

...