In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand.
value1 = 8 value2 = 2 remainder = value1 % value2 print(remainder)
Subsequent to composing the above code (what does the per cent sign mean in python), Ones you will print the " leftover portion ", then, at that point, the result will show up as a " 0 ". Here, the modulo operator " % " returns the rest of isolating the two numbers.
The % operator advises the Python mediator to design a string utilizing a given arrangement of factors encased in a tuple, following the administrator. An exceptionally basic illustration of this is the following:
'%s is smaller than %s' % ('one', 'two')
The Python translator substitutes the main event of %s in the string by the given string "one" and the second %s by the string "two". These %s strings are really placeholders in our "layout" string, and they show that strings will be put there.
As a first model, beneath, we show utilizing the Python REPL how to print string esteem and float esteem:
print("Mr. %s, the total is %.2f." % ("Jekyll", 15.53))
Very much like the %s is a placeholder for strings, %f is a placeholder for drifting point numbers. The ".2" preceding the f shows the number of digits we that need showed after the decimal point.
These are only two basic instances of what is conceivable, and much more placeholder types are upheld. Here is the full rundown of placeholder types in more detail:
This placeholder addresses a solitary person.
print("The character after %c is %c." % ("A", "C"))
Giving in excess of a solitary person as the variable here will raise an exemption.
This placeholder utilizes string change through str() preceding designing. So any worth that can be changed over completely to a string through str() can be utilized here.
place = "India" print("Welcome to %s!" % place)
Here we just have a solitary component to be utilized in our string designing, and in this way, we're not expected to encase the component in a tuple like the past models.
%I and %d
These placholders address a marked decimal whole number.
y = 2019 print("%i will be a perfect year." % y)
Since this placeholder expects a decimal, it will be changed over completely to one in the event that drifting point esteem is given all things being equal.
This placeholder addresses an unsigned decimal whole number.
This placeholder addresses an octal whole number.
num = 15 print("%i in octal is %o" % (num, num))
Addresses a whole hexadecimal number utilizing lowercase letters (a-f).
number = 15 print("%i in hex is %02x" % (number, number))
By utilizing the "02" prefix in our placeholder, we're advising Python to print a two-character hex string.
Addresses a whole hexadecimal number utilizing capitalized letters (A-F).
number = 15 print("%i in hex is %04X" % (number, number))
What's more, similar to the past model, by utilizing the "04" prefix in our placeholder, we're advising Python to print a four-character hex string.
Addresses dramatic documentation with a lowercase "e".
Addresses dramatic documentation with a capitalized "e".
Addresses a drifting point genuine number.
pr= 15.95 print("the price is %.2f" % pr)
The more limited adaptation of %f and %e.
The more limited adaptation of %f and %E.
The placeholders displayed above permit you to arrange strings by determining information types in your formats. Be that as it may, these aren't the main elements of the addition administrator. In the following subsection, we'll perceive the way we can cushion our strings with spaces utilizing the % administrator.
Adjusting the Output
As of recently, we've just been told the best way to arrange text strings is by indicating basic placeholders. With the assistance of extra mathematical worth, you can characterize the all-out space that will be held on one or the other side of a variable in the result string.
As an illustration, the worth of %10s saves ten characters, with the additional separating on the left half of the placeholder, and the worth of %-10s puts any additional room to the right of the placeholder. The single cushioning character is a space and can't be changed.
place = "London" print ("%10s is not a place in France" % place) # Pad to the left
place = "London" print ("%-10s is not a place in France" % place) # Pad to the right
Managing numbers works similarly:
print ("The postcode is %10d." % 25000) # Padding on the left side
print ("The postcode is %-10d." % 25000) # Padding on the right side