Hướng dẫn python string percent sign

💡 Outline

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

See the code below:

val="The percentage is 92.27"

print("%s%%"%val)    

Output:

The percentage is 92.27%

Table of Contents

  • Introduction
  • Print Percentage Sign in Python
    • Using %% Character to Escape Percent Sign in Python
    • Using the format() Function
    • Using the f-String
    • Was this post helpful?

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,

a=10%3

print("The remainder is %d"%a)    

Output:

The remainder is 1

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.

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,

val="The percentage is 95.68%"

print(val)    

Output:

The percentage is 95.68%

However, we get an error if we aim to print the percent sign (%) while using format specifiers.

See the code below.

val="The percentage is 95.68"

print("%s%"%val)    

Output:

ValueError: incomplete format

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.

val="The percentage is 95.68"

print("%s%%"%val)    

Output:

The percentage is 95.68%

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,

Output:

95.68%%

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,

val="The percentage is 95.68"

print("{}%".format(val))    

Output:

The percentage is 95.68%

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,

val="The percentage is 95.68"

print(f"{val}%")    

Output:

The percentage is 95.68%

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.