Print percentage sign in python

Python print percent sign

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 the 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 the %% character

To print the 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.

Related Posts

  • How to comment out multiple lines in python
    16 October

    How to comment out multiple lines in python

    Table of ContentsMultiple Line Comments in PythonUsing the # characterUsing triple quotesUsing IDEs and Text Editors In this article, we will see about how to comment out multiple lines in python. The concept of comments is present in most programming languages. We use comments for documentation purposes. The compiler ignores them, but the user can […]

  • Get variable name as String in Python
    01 October

    How to get variable name as String in Python

    Table of ContentsWhat are variable names in Python?How to get a variable name as a string in Python?In Python 2.xIn Python 3.x In Python, there are several ways to convert a string to a variable name, but the reverse process is not exactly utilized so often in the programming world. This article focuses on how […]

  • Print blank line in Python
    23 September

    Print blank line in Python

    Table of ContentsWhat is a blank line in Python?Ways to print blank line in PythonUsing an empty print() functionUsing the print() function with newline characterUsing the print() function with an empty stringUsing the print() function with the sep parameterWays to print multiple blank lines in PythonUsing the print() function with multiple newline charactersUsing the print() […]

  • 17 September

    Write list to CSV in Python

    Table of ContentsUsing the write() function to write a list to a CSV fileUsing the csv module to write a list to a CSV fileUsing the numpy module to write a list to a CSV fileUsing the numpy.savetxt() function to write a list to a CSV fileUsing the numpy.tofile() function to write a list to […]

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • 10 September

    Change the font size of title in Matplotlib

    In Python, there are a lot of libraries that are used for data visualization. One of the libraries is the Matplotlib library. The Matplotlib library is a very good library when it comes to data visualisation in python. With the help of this library, many types of graphs and visual representations of the data can […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.