Hướng dẫn python print percent sign
Table of Contents
IntroductionIn this tutorial, we will see how to print percentage sign in python. The modulus operator ( 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,
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 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,
Output: The percentage is 95.68% However, we get an error if we aim to print the percent sign ( See the code below.
Output: ValueError: incomplete format In the above example, we can see that we wish to print Now let us understand how to escape percent sign in Python. Using %% Character to Escape Percent Sign in PythonTo print percentage sign, we can escape using the percentage sign twice instead of once. See the code below.
Output: The percentage is 95.68% By using the percentage sign twice ( For example, Output: 95.68%% Using the format() FunctionThe For example,
Output: The percentage is 95.68% Using the f-StringThe For example,
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. |