💡

OutlineTo print percentage sign in Python, we can escape using the percentage sign twice instead of once.

See the code below:

1234 val = "The percentage is 92.27"print("%s%%"%val)

Output:The percentage is 92.27%

Table of Contents

## Introduction

In this tutorial, we will see how to print percentage sign in python.

The modulus operator (`%`

) or the percentage sign has multiple usages in Python. We use this operator to calculate the remainder for the division between two values for the arithmetic purpose.

We can also use the percentage sign for various string operations. We use it in string formatting and replacing values using format specifiers.

For example,

1 2 3 4 |
a = 10 % 3 print("The remainder is %d"%a) |

Output:

In the above example, we first find the remainder of 10 divided by 3 and store it in a variable `a`

. Then, we have an integer in the `print()`

function, which we display using the `%d`

format specifier. There are different specifiers for different formats. The `%s`

indicates a string, `%f`

indicates float value, and more.

## Print Percentage Sign in Python

Now we may think about what issue might arise while printing the percentage sign. If we print it normally, there seems to be no problem.

For example,

1 2 3 4 |
val = "The percentage is 95.68%" print(val) |

Output:

However, we get an error if we aim to print the percent sign (`%`

) while using format specifiers.

See the code below.

1 2 3 4 |
val = "The percentage is 95.68" print("%s%"%val) |

Output:

In the above example, we can see that we wish to print `95.68%`

using the `%s`

format specifier to provide the value from the string variable `val`

in the `print()`

function. We need to escape the percent sign to display it selectively.

Now let us understand how to escape percent sign in Python.

### Using `%%`

Character to Escape Percent Sign in Python

To print percentage sign, we can escape using the percentage sign twice instead of once.

See the code below.

1 2 3 4 |
val = "The percentage is 95.68" print("%s%%"%val) |

Output:

By using the percentage sign twice (`%%`

), we can overcome the error. However, if we are not using any specifier, this will print the percentage sign twice only.

For example,

1 2 3 |
print("95.68%%") |

Output:

### Using the `format()`

Function

The `format()`

function can help us to format complex strings with ease. To escape the percent sign, we can place the string variable within curly braces and display the sign separately.

For example,

1 2 3 4 |
val = "The percentage is 95.68" print("{}%".format(val)) |

Output:

### Using the `f-String`

The `f-strings`

are a new addition to Python 3.6 and up and allow us to format strings faster and more efficiently. The format will be the same as discussed in the previous method.

For example,

1 2 3 4 |
val = "The percentage is 95.68" print(f"{val}%") |

Output:

In this article, we demonstrate the use and function of the percentage sign in Python. With the help of examples, we can understand the errors associated with printing the percent sign and how to overcome them. We also understand a little about string formatting, which is an essential technique in Python to get the result in our desired format.