What Does the Percent Sign (%) Mean in Python?
In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand.
Example:
value1 = 8
value2 = 2
remainder = value1 % value2
print(remainder)
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python1.png)
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))
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python2.png)
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:
%c
This placeholder addresses a solitary person.
print("The character after %c is %c." % ("A", "C"))
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python3.png)
Giving in excess of a solitary person as the variable here will raise an exemption.
%s
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)
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python4.png)
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)
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python5.png)
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.
%u
This placeholder addresses an unsigned decimal whole number.
%o
This placeholder addresses an octal whole number.
num = 15
print("%i in octal is %o" % (num, num))
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python6.png)
%x
Addresses a whole hexadecimal number utilizing lowercase letters (a-f).
number = 15
print("%i in hex is %02x" % (number, number))
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python7.png)
By utilizing the "02" prefix in our placeholder, we're advising Python to print a two-character hex string.
%X
Addresses a whole hexadecimal number utilizing capitalized letters (A-F).
number = 15
print("%i in hex is %04X" % (number, number))
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python8.png)
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.
%e
Addresses dramatic documentation with a lowercase "e".
%E
Addresses dramatic documentation with a capitalized "e".
%f
Addresses a drifting point genuine number.
pr= 15.95
print("the price is %.2f" % pr)
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python9.png)
%g
The more limited adaptation of %f and %e.
%G
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
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python10.png)
place = "London"
print ("%-10s is not a place in France" % place) # Pad to the right
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python11.png)
Managing numbers works similarly:
print ("The postcode is %10d." % 25000) # Padding on the left side
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python12.png)
print ("The postcode is %-10d." % 25000) # Padding on the right side
Output:
![What Does the Percent Sign (%) Mean in Python?](https://static.tutorialandexample.com/python/what-does-the-percent-sign-mean-in-python13.png)