Print Percentage Sign in Python

Python print percent sign

💡 Outline

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

See the code below:

Output:

The percentage is 92.27%

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,

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.

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,

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.

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.

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,

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,

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.


import_contacts

You may also like:

Related Posts

  • Make requirements.txt in Python
    03 May

    Make requirements.txt in python

    Table of ContentsWhat is the requirements.txt file in Python?Ways to make requirements.txt file in PythonUsing the pip to make requirements.txt in PythonUsing the conda command to make requirements.txt file in PythonUsing the pipreqs package to make requirements.txt file in PythonConclusion What is the requirements.txt file in Python? Every package is also updated regularly and features […]

  • Create an array of 1 to 10 in Python
    03 May

    Create an Array of 1 to 10 in Python

    Table of ContentsIntroductionHow to create an array of 1 to 10 in Python?Using the range() function to create an array of 1 to 10 in Python.Using list comprehension along with the range() function to create an array of 1 to 10 in Python.Using a user-defined function to create an array of 1 to 10 in […]

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • Return vs Print in Python
    16 April

    Return vs Print in Python

    Table of ContentsReturn vs Print in PythonConclusion Return vs Print in Python In Python, we work with a wide range of functions that make our code simple. We can also create functions using the def keyword. A function can be defined as per our requirement and can display something or return a value. This is […]

  • Floor division in Python
    10 April

    Floor division in Python

    Table of ContentsWhat is Floor Division in Python?How to implement floor division in Python?Using the // operator to implement floor division in Python.Using the // operator to implement floor division on negative integers in Python.Using the // operator to implement floor division on floating-point numbers in Python.Using the // operator to implement floor division with […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.