What does the percent sign mean in python

By: On:

In this Python tutorial, we will discuss what does the percent sign mean in python. We will also check some examples:

  • What does the percent sign mean in python
  • What does %s mean in python
  • Python modulo with integer
  • Python modulo with float
  • Python zero division error
  • Python modulo with negative numbers

What does the percent sign mean in python

In python, the percent sign is called modulo operator ” % “ which returns the remainder after dividing the left-hand operand by right-hand operand.


Example:

value1 = 8
value2 = 2
remainder = value1 % value2
print(remainder)

After writing the above code (what does the percent sign mean in python), Ones 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 what does the percent sign mean in python.

What does the percent sign mean in python
percentage symbol mean in python

What does %s mean in python

The %s operator allows you to add value into a python string. The %s signifies that you want to add string value into the string, it is also used to format numbers in a string.


Example:

var = '20'
string = "Variable as string = %s" %(var)
print(string)

After writing the above code (what does %s mean in python), Ones you will print ” string “ then the output will appear as a “ Variable as string = 20 ”. Here, the ” %s “ is used for adding string value and it converts the value to a string.

You can refer to the below screenshot what does %s mean in python

What does %s mean in python
What does %s mean in python

Python modulo with integers

In python, when both the number is integer, then the remainder is also an integer value.

Example:

integer1 = 10
integer2 = 5
remainder = integer1 % integer2
print(remainder)

After writing the above code (python modulo with integers), Ones you will print ” remainder “ then the output will appear as a “ 0 ”. Here, the modulo operator ” % “ returns the remainder after dividing the two numbers which are integer1 and integer2.

Here is the below screenshot python modulo with integers

Python modulo with integers

Python modulo with float

In python, if one of the number is float, then the output will be floating point number.

Example:

float1 = 10
float2 = 3.0
remainder = float1 % float2
print(remainder)

After writing the above code (python modulo with float), Ones you will print ” remainder “ then the output will appear as a “ 1.0 ”. Here, the modulo operator ” % “ returns the remainder as a floating-point number after dividing the two numbers which are float1 and float2.

You can refer to the below screenshot python modulo with float

Python modulo with float

Python zero division error

In python, we get this ZeroDivisionError if the divisor is 0, then the module operator will throw this error because we cannot divide by 0.

Example:

value1 = 8
value2 = 0
remainder = value1 % value2
print(remainder)

After writing the above code (python zero division error), Ones you will print ” remainder “ then the output will appear as a “ ZeroDivisionError: integer division or modulo by zero ”. Here, the modulo operator ” % “ will throw an error if we divide it by 0.

Here is the screenshot python zero division error.

Python zero division error

Python modulo with negative numbers

In python, the modulo operator will always give the remainder having the same sign as the divisor.

Example:

value1 = -11
value2 = 3
remainder = value1 % value2
print(remainder)

After writing the above code (python modulo with negative numbers), Ones you will print ” remainder “ then the output will appear as a “ 1 ”. Here, the remainder will have the same sign as the divisor, so my divisor is positive that’s why the remainder is also positive vice versa.

Below screenshot python modulo with negative numbers.

Python modulo with negative numbers

You may like the following Python tutorials:

In this tutorial, we learned what does the percent sign mean in python with various example.

  • What does the percent sign mean in python
  • What does %s mean in python
  • Python modulo with integer
  • Python modulo with float
  • Python zero division error
  • Python modulo with negative numbers

Leave a Comment