Table of Contents
In this article, we will cover what is % in Python and what are different ways to use % in Python.
In python, there are different types of operators with which we can perform different operations on any given data. In this article, we will discuss the % operator in python. We will also discuss the various applications of the % operator in python.
What is % Operator in Python?
The % operator is known as the modulo operator in python. It is a binary operator that operates on two operands. The syntax for the % operator is as follows.
1 2 3 |
result = expression1 % expression2 |
Here, expression1
and expression2
must evaluate to a numeric value. The numeric value may be an integer, a floating-point number. After execution, “expression1 % expression2
” returns the remainder when expression1
is divided by expression2
.
For instance, 17%3 will evaluate to 2
. This is so because when 17 is divided by 3, we get 2 as the remainder. To understand the working of the % operator in a better way, look at the following example.
1 2 3 4 5 6 |
num1 = 17 num2 = 3 result = num1 % num2 print("The result of {} % {} is {}.".format(num1, num2, result)) |
Output:
1 2 3 |
The result of 17 % 3 is 2. |
The % Operator with strings in python
The % operator is also used for string formatting in python. There may be situations when we need to insert a variable into a string. In such situations, we use the % operator inside the strings as a placeholder for variables or to specify the format of the variables in the string. Suppose that you are given the name “Aditya
” and the age “23
” as variables. Now, we have to print the string "Aditya is 23 years old.
“. We can use them in a string as discussed below.
The % Operator as a placeholder for variables in python
For instance, suppose that we have to print the name and age of a person that has been given as variables name
and age
. To fit the variables into the sentence, we will use the % operator as follows.
1 2 3 |
"%s is %d years old." % (name,age) |
Here, inside the string, we have used the % operator as placeholders. The specifier %s
shows that a string value will be placed at the place of %s
. Similarly, %d
will be replaced with an integer.
The % operator when used outside the string, accepts a tuple of variables and inserts the values at the specified placeholders as follows.
1 2 3 4 5 6 7 |
name = "Aditya" age = 23 output = "%s is %d years old." % (name, age) print(output) |
Output:
1 2 3 4 |
Aditya is 23 years old. |
Remember that the tuple containing the variables should have as many variables as the number of placeholders in the string. Otherwise, the program will run into a TypeError
exception.
For instance, if the tuple of variables has more variables than the number of placeholders in the string, the program, on execution, will run into the TypeError
exception as shown below.
1 2 3 4 5 6 7 |
name = "Aditya" age = 23 weight = 68 output = "%s is %d years old." % (name, age, weight) print(output) |
Output::
1 2 3 4 5 6 |
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module> output = "%s is %d years old." % (name, age, weight) TypeError: not all arguments converted during string formatting |
Here, you can observe that the TypeError
exception has been raised by the program with the message “TypeError: not all arguments converted during string formatting"
when we tried to pass more variables than the number of placeholders in the string.
Similarly, when we pass a lesser number of variables in the tuple compared to the placeholders, the program will run into an error causing the TypeError exception as shown below.
1 2 3 4 5 6 7 |
name = "Aditya" age = 23 weight = 68 output = "%s is %d years old." % (name) print(output) |
Output:
1 2 3 4 5 6 |
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module> output = "%s is %d years old." % (name) TypeError: not enough arguments for format string |
Here, when we tried to pass a lesser number of variables than the number of placeholders in the string, the program raised the TypeError
exception with the message “TypeError: not enough arguments for format string
“.
You should also maintain the order of the variables in the tuple. The variables in the tuple should be compatible with the corresponding placeholder in the string. Otherwise, the program will run into a TypeError
exception.
For instance, the %s
specifier is used as a placeholder for string variables. Similarly, the %d
specifier is used as a placeholder for the integers. If we pass an integer at the place of the string variable or a string in place of the integer, the program will run into a TypeError
exception as shown below.
1 2 3 4 5 6 7 |
name = "Aditya" age = 23 weight = 68 output = "%s is %d years old." % (age, name) print(output) |
Output:
1 2 3 4 5 6 |
Traceback (most recent call last): File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 4, in <module> output = "%s is %d years old." % (age, name) TypeError: %d format: a number is required, not str |
The % Operator as format specifiers in python
We can also use the % operator to specify the format of numbers in the strings. As discussed above, we can insert a variable into a string using the % operator.
In the case of integers or strings, generally, we do not need to specify the format of the value. We simply insert the value from the variable using the placeholders. However, we might need to specify the width of the values for better alignment or to specify the number of digits in a floating-point value. In such cases, we can use the % operator to specify the format of the strings in python. The syntax for specifying the format is as follows.
1 2 3 |
%[width][.precision]type |
Here, width
is the total length the value from the variable can take. precision
is the number of digits after the decimal point (in the case of numbers), and the type
specifies the data type of the value in the variable that is inserted into the string.
For instance, we can specify the formatting of a floating-point number using the % operator by limiting the number of digits to 2 and precision to 1 digit as follows.
1 2 3 4 5 6 7 8 9 |
num1 = 17 num2 = 3 result = 17 / 3 str1 = "The result of %d / %d is %f ." % (num1, num2, result) str2 = "The result of %d / %d restricted to 2 digits and 1 decimal digits is %2.1f ." % (num1, num2, result) print(str1) print(str2) |
Output:
1 2 3 4 |
The result of 17 / 3 is 5.666667 . The result of 17 / 3 restricted to 2 digits and 1 decimal digits is 5.7 . |
We can also define the width of the string values using the width field. But precision is not defined for strings.
1 2 3 4 5 6 7 |
name = "Aditya" age = 23 weight = 68 output = "%15s is %d years old." % (name, age) print(output) |
Output:
1 2 3 |
Aditya is 23 years old. |
In the above output, you can see that we have defined the width of the name
to be 15. Hence, the name “Aditya
” has been padded using spaces. However, if we define a width less than the actual string length, there is no effect on the string.
1 2 3 4 5 6 7 |
name = "Aditya" age = 23 weight = 68 output = "%2s is %d years old." % (name, age) print(output) |
Output:
1 2 3 |
Aditya is 23 years old. |
Here, you can observe that we have defined the width of the variable name
to be 2 characters. However, “Aditya
” consists of 6 characters. Hence, no change is observed in the output.
Further reading:
Conclusion
In this article, we have discussed the % operator in python. We also saw different applications and use cases of the % operator in python in formatting strings.
That’s all about what is % in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!