In this Python tutorial, we will discuss everything about, the Python modulo operator, and what does the percent (%) sign mean in Python.
What does the percent sign mean in Python?
In Python, the percent sign is called the modulo operator ” % ” which returns the remainder after dividing the left-hand operand by the right-hand operand.
Example:
value1 = 8
value2 = 2
remainder = value1 % value2
print(remainder)
Once you will print ” remainder “ then the output will appear as a “ 0 ”. Here, the modulo operator ” % “ returns the remainder after dividing the two numbers.
You can refer to the below screenshot:
Let us explore, different uses of the percent sign in Python.
Modulo Operator in Python
In Python, the percent sign (%) is frequently used as the modulo operator. The modulo operator calculates the remainder of a division operation.
Let’s take an example:
remainder = 10 % 3
print(remainder) # Output: 1
In the above example, 10 % 3
computes the remainder when 10
is divided by 3
. Here, 10
is the dividend, 3
is the divisor, and 1
is the remainder. Thus, the operation 10 % 3
returns 1
.
This operation is particularly useful in programming scenarios where you need to cycle through a sequence. It can also be used to determine if a number is even or odd. For instance:
number = 7
if number % 2 == 0:
print(f"{number} is even.")
else:
print(f"{number} is odd.")
# Output: 7 is odd.
In the above code, we use the modulo operator to check if the number is divisible by 2 (i.e., if it’s an even number). If number % 2
equals 0
, then the number is even; otherwise, it’s odd. You can see the output below:
Percent symbol (%) for string formatting
Percentage sign in Python is also used for string formatting. It is used to inject data into your strings. The %
operator takes a format string and an argument or tuple of arguments. The format string includes %d
for integers, %s
for strings, and %f
for floating-point numbers.
Here’s an example:
name = 'John'
age = 30
print('Hello, my name is %s and I am %d years old.' % (name, age))
# Output: Hello, my name is John and I am 30 years old.
In the example, %s
is replaced by the string ‘John’ and %d
is replaced by the integer 30.
Conclusion
The percent sign (%) in Python serves multiple functionalities that can significantly aid in enhancing the efficiency of your code. From acting as a modulo operator to offering a way for string formatting, the %
operator proves to be quite a versatile tool in the Python programming language. I hope you got an idea about, percentage symbol in Python.
You may also like:
- How to get current directory name in Python
- sum of two numbers in Python using a function
- remove unicode characters from string python
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.