This article will discuss the % (Python percent sign) in Python. Usually, it represents the integer, decimal, string, etc., type variables. On the other hand, it will return the remainder of the two numbers.
So, let’s see the two scenarios in this article.
Scenario 1: Using % or Python percent sign as a formatting operator
We specified that the % operator represents the integer, decimal, string, etc., type variables.
Let’s see different formatting operators with an example for each.
1. %c
This formatting operator specifies the character, and character means single string value.
Syntax:
print("%c" %'character')
where character specifies a single string.
Example:
In this example, we will print different characters using %c.
#demo for %c formatting operator that prints A print ("Hello Welcome , This is %c " %'A') #demo for %c formatting operator that prints B print ("Hello Welcome , This is %c " %'B') #demo for %c formatting operator that prints C print ("Hello Welcome , This is %c " %'C') #demo for %c formatting operator that prints D print ("Hello Welcome , This is %c " %'D') #demo for %c formatting operator that prints E print ("Hello Welcome , This is %c " %'E')
Output:
Hello Welcome , This is A Hello Welcome , This is B Hello Welcome , This is C Hello Welcome , This is D Hello Welcome , This is E
2. %s
This formatting operator specifies the string, which is a collection of characters.
Syntax:
print("%s" %'string')
Where the string specifies the string as a value.
Example:
In this example, we will print different characters using %s.
#demo for %s formatting operator that prints Python print ("Hello Welcome , This is %s " %'Python') #demo for %s formatting operator that prints java print ("Hello Welcome , This is %s " %'java') #demo for %s formatting operator that prints C print ("Hello Welcome , This is %s " %'C') #demo for %s formatting operator that prints DBMS print ("Hello Welcome , This is %s " %'DBMS') #demo for %s formatting operator that prints html print ("Hello Welcome , This is %s " %'html')
Output:
Hello Welcome , This is Python Hello Welcome , This is java Hello Welcome , This is C Hello Welcome , This is DBMS Hello Welcome , This is html
Also Read: How To Display A Calendar In Python
3. %i
This formatting operator is used to specify the signed decimal integer.
It can represent positive and negative numbers as well.
Syntax:
print("%i" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %i.
#demo for %i formatting operator that prints 34 print ("Hello Welcome , This is %i " %34) #demo for %i formatting operator that prints +89 print ("Hello Welcome , This is %i " %+89) #demo for %i formatting operator that prints 0 print ("Hello Welcome , This is %i " %0) #demo for %i formatting operator that prints -678 print ("Hello Welcome , This is %i " %-678) #demo for %i formatting operator that prints 43 print ("Hello Welcome , This is %i " %43)
Output:
Hello Welcome , This is 34 Hello Welcome , This is 89 Hello Welcome , This is 0 Hello Welcome , This is -678 Hello Welcome , This is 43
4. %d
This formatting operator is used to specify the signed decimal integer in Python. It is similar to %i.
It can represent positive and negative numbers as well.
Syntax:
print("%d" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %d.
#demo for %d formatting operator that prints 34 print ("Hello Welcome , This is %d " %34) #demo for %d formatting operator that prints +89 print ("Hello Welcome , This is %d " %+89) #demo for %d formatting operator that prints 0 print ("Hello Welcome , This is %d " %0) #demo for %d formatting operator that prints -678 print ("Hello Welcome , This is %d " %-678) #demo for %d formatting operator that prints 43 print ("Hello Welcome , This is %d " %43)
Output:
Hello Welcome , This is 34 Hello Welcome , This is 89 Hello Welcome , This is 0 Hello Welcome , This is -678 Hello Welcome , This is 43
5. %u
This formatting operator is used to specify the unsigned decimal integer.
It can represent positive and negative numbers as well.
Syntax:
print("%u" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %u.
#demo for %u formatting operator that prints 34 print ("Hello Welcome , This is %u " %34) #demo for %u formatting operator that prints +89 print ("Hello Welcome , This is %u " %+89) #demo for %u formatting operator that prints 0 print ("Hello Welcome , This is %u " %0) #demo for %u formatting operator that prints -678 print ("Hello Welcome , This is %u " %-678) #demo for %u formatting operator that prints 43 print ("Hello Welcome , This is %u " %43)
Output:
Hello Welcome , This is 34 Hello Welcome , This is 89 Hello Welcome , This is 0 Hello Welcome , This is -678 Hello Welcome , This is 43
Also Read: How to Create Python Empty Set
6. %o
This formatting operator specifies the octal value for the given value.
It can represent positive and negative numbers as well.
Syntax:
print("%o" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %i and get the octal values concerning the integer values using %o.
#demo for %0 formatting operator that prints the octal value for the value 1 print ("Hello Welcome , This is %i and my octal representation is %o" %(1,1)) #demo for %0 formatting operator that prints the octal value for the value 634 print ("Hello Welcome , This is %i and my octal representation is %o" %(634,634)) #demo for %0 formatting operator that prints the octal value for the value 0 print ("Hello Welcome , This is %i and my octal representation is %o" %(0,0)) #demo for %0 formatting operator that prints the octal value for the value 1000 print ("Hello Welcome , This is %i and my octal representation is %o" %(1000,1000)) #demo for %0 formatting operator that prints the octal value for the value -90 print ("Hello Welcome , This is %i and my octal representation is %o" %(-90,-90))
Output:
Hello Welcome , This is 1 and my octal representation is 1 Hello Welcome , This is 634 and my octal representation is 1172 Hello Welcome , This is 0 and my octal representation is 0 Hello Welcome , This is 1000 and my octal representation is 1750 Hello Welcome , This is -90 and my octal representation is -132
7. %x
This formatting operator specifies the hexadecimal value for the given value in lowercase.
It can represent positive and negative numbers as well.
Syntax:
print("%x" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %i and get the hexadecimal values concerning the integer values using %x.
#demo for %0 formatting operator that prints the hexadecimal value for the value 1 print ("Hello Welcome , This is %i and my hexadecimal representation is %x" %(1,1)) #demo for %0 formatting operator that prints the hexadecimal value for the value 634 print ("Hello Welcome , This is %i and my hexadecimal representation is %x" %(634,634)) #demo for %0 formatting operator that prints the hexadecimal value for the value 0 print ("Hello Welcome , This is %i and my hexadecimal representation is %x" %(0,0)) #demo for %0 formatting operator that prints the hexadecimal value for the value 1000 print ("Hello Welcome , This is %i and my hexadecimal representation is %x" %(1000,1000)) #demo for %0 formatting operator that prints the hexadecimal value for the value -90 print ("Hello Welcome , This is %i and my hexadecimal representation is %x" %(-90,-90))
Output:
Hello Welcome , This is 1 and my hexadecimal representation is 1 Hello Welcome , This is 634 and my hexadecimal representation is 27a Hello Welcome , This is 0 and my hexadecimal representation is 0 Hello Welcome , This is 1000 and my hexadecimal representation is 3e8 Hello Welcome , This is -90 and my hexadecimal representation is -5a
Also Read: How To Create An Empty Dictionary in Python
8. %X
This formatting operator is used to specify the hexadecimal value for the given value in the upper case.
It can represent positive and negative numbers as well.
Syntax:
print("%X" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %i and get the hexadecimal values concerning the integer values using %X.
#demo for %0 formatting operator that prints the hexadecimal value for the value 1 print ("Hello Welcome , This is %i and my hexadecimal representation is %X" %(1,1)) #demo for %0 formatting operator that prints the hexadecimal value for the value 634 print ("Hello Welcome , This is %i and my hexadecimal representation is %X" %(634,634)) #demo for %0 formatting operator that prints the hexadecimal value for the value 0 print ("Hello Welcome , This is %i and my hexadecimal representation is %X" %(0,0)) #demo for %0 formatting operator that prints the hexadecimal value for the value 1000 print ("Hello Welcome , This is %i and my hexadecimal representation is %X" %(1000,1000)) #demo for %0 formatting operator that prints the hexadecimal value for the value -90 print ("Hello Welcome , This is %i and my hexadecimal representation is %X" %(-90,-90))
Output:
Hello Welcome , This is 1 and my hexadecimal representation is 1 Hello Welcome , This is 634 and my hexadecimal representation is 27A Hello Welcome , This is 0 and my hexadecimal representation is 0 Hello Welcome , This is 1000 and my hexadecimal representation is 3E8 Hello Welcome , This is -90 and my hexadecimal representation is -5A
Also Read: How to Exit Python Program [4 Methods]
9. %e
This formatting operator specifies the exponential value for the given value in lowercase.
It can represent positive and negative numbers as well.
Syntax:
print("%e" %value)
Where the value specifies the positive/negative/zero as the value.
Example:
In this example, we will print different integer values using %i and get the exponential values concerning the integer values using %e.
#demo for %0 formatting operator that prints the exponential value for the value 1 print ("Hello Welcome , This is %i and my exponential representation is %e" %(1,1)) #demo for %0 formatting operator that prints the exponential value for the value 634 print ("Hello Welcome , This is %i and my exponential representation is %e" %(634,634)) #demo for %0 formatting operator that prints the exponential value for the value 0 print ("Hello Welcome , This is %i and my exponential representation is %e" %(0,0)) #demo for %0 formatting operator that prints the exponential value for the value 1000 print ("Hello Welcome , This is %i and my exponential representation is %e" %(1000,1000)) #demo for %0 formatting operator that prints the exponential value for the value -90 print ("Hello Welcome , This is %i and my exponential representation is %e" %(-90,-90))
Output:
Hello Welcome , This is 1 and my exponential representation is 1.000000e+00 Hello Welcome , This is 634 and my exponential representation is 6.340000e+02 Hello Welcome , This is 0 and my exponential representation is 0.000000e+00 Hello Welcome , This is 1000 and my exponential representation is 1.000000e+03 Hello Welcome , This is -90 and my exponential representation is -9.000000e+01
10. %E
This formatting operator specifies the exponential value for the given value in the upper case.
It can represent positive and negative numbers as well.
Syntax:
print("%E" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %i and get the exponential values concerning the integer values using %E.
#demo for %0 formatting operator that prints the exponential value for the value 1 print ("Hello Welcome , This is %i and my exponential representation is %E" %(1,1)) #demo for %0 formatting operator that prints the exponential value for the value 634 print ("Hello Welcome , This is %i and my exponential representation is %E" %(634,634)) #demo for %0 formatting operator that prints the exponential value for the value 0 print ("Hello Welcome , This is %i and my exponential representation is %E" %(0,0)) #demo for %0 formatting operator that prints the exponential value for the value 1000 print ("Hello Welcome , This is %i and my exponential representation is %E" %(1000,1000)) #demo for %0 formatting operator that prints the exponential value for the value -90 print ("Hello Welcome , This is %i and my exponential representation is %E" %(-90,-90))
Output:
Hello Welcome , This is 1 and my exponential representation is 1.000000E+00 Hello Welcome , This is 634 and my exponential representation is 6.340000E+02 Hello Welcome , This is 0 and my exponential representation is 0.000000E+00 Hello Welcome , This is 1000 and my exponential representation is 1.000000E+03 Hello Welcome , This is -90 and my exponential representation is -9.000000E+01
11. %f
This formatting operator is used to specify the float value for the given value,
It can represent positive and negative numbers as well.
Syntax:
print("%f" %value)
Where value specifies the positive/negative/zero as value.
Example:
In this example, we will print different integer values using %i and get the float values.
#demo for %0 formatting operator that prints the float value for the value 1 print ("Hello Welcome , This is %i and my float representation is %f" %(1,1)) #demo for %0 formatting operator that prints the float value for the value 634 print ("Hello Welcome , This is %i and my float representation is %f" %(634,634)) #demo for %0 formatting operator that prints the float value for the value 0 print ("Hello Welcome , This is %i and my float representation is %f" %(0,0)) #demo for %0 formatting operator that prints the float value for the value 1000 print ("Hello Welcome , This is %i and my float representation is %f" %(1000,1000)) #demo for %0 formatting operator that prints the float value for the value -90 print ("Hello Welcome , This is %i and my float representation is %f" %(-90,-90))
Output:
Hello Welcome , This is 1 and my float representation is 1.000000 Hello Welcome , This is 634 and my float representation is 634.000000 Hello Welcome , This is 0 and my float representation is 0.000000 Hello Welcome , This is 1000 and my float representation is 1000.000000 Hello Welcome , This is -90 and my float representation is -90.000000
Scenario 2: Using % or Python percent sign to get the remainder
We will get the quotient when a dividend is divided (/) by divisor. If we use %, we will get the remainder.
Syntax:
dividend%divisor
Example:
In this example, we will get the remainder performed on two numbers.
#get the remainder when dividend is 45 and divisor is 8 print(45%8) #get the remainder when dividend is 145 and divisor is 38 print(145%38) #get the remainder when dividend is 1 and divisor is 1 print(1%1) #get the remainder when dividend is 0 and divisor is 45 print(0%45)
Output:
5 31 0 0
This tutorial showed us how to use the % symbol in Python with all possible scenarios.