Percentage Symbol (%) in Python

In this tutorial, I will explain how to use the percentage (%) symbol in Python for string formatting and interpolation. The percentage sign has a special meaning in Python when used in the context of strings. It allows you to insert values into a string template using a technique called string interpolation.

The percentage symbol (%) in Python is primarily used as the modulo operator to calculate the remainder when one number is divided by another. It also has applications in string formatting for inserting values into string templates using placeholders and specifiers.

Percentage Symbol (%) in Python

The percentage symbol (%) in Python has two primary uses: as a modulus operator and for string formatting.

Use the Percentage Symbol as a Modulus Operator

The modulus operator (%) is used to find the remainder of a division between two numbers. This can be particularly useful in various programming scenarios, such as determining whether a number is even or odd.

Example: Determining Even or Odd Numbers

Let’s say you want to find out if a given number is even or odd. You can use the modulus operator to achieve this:

number = 25
if number % 2 == 0:
    print(f"{number} is even")
else:
    print(f"{number} is odd")

In this example, the output will be:

25 is odd

Here is the exact output in the screenshot below:

percentage symbol in python

Check out Add Two Numbers in Python

String Interpolation with the Percent Operator

In Python, you can use the percent (%) operator to construct strings with a template string and variables containing your data. This is known as string interpolation. The % operator is a convenient way to format strings by substituting values into placeholders within the string.

Here’s an example:

name = "John"
age = 25
city = "New York"

print("My name is %s, I am %d years old, and I live in %s." % (name, age, city))

Output:

My name is John, I am 25 years old, and I live in New York.

In this example, the %s placeholder is used for strings, %d for integers, and the values are provided in a tuple after the % operator.

Here is the exact output in the screenshot below:

python percentage sign

Read Increment and Decrement operators in Python

Formatting Specifiers

The percent sign can be followed by various formatting specifiers to control how the values are formatted. Some commonly used specifiers include:

  • %s: String
  • %d: Integer
  • %f: Floating-point number
  • %.<number>f: Floating-point number with a fixed number of digits after the decimal point

Here’s an example demonstrating the usage of formatting specifiers:

price = 19.99

print("The price is %.2f USD" % price)

Output:

The price is 19.99 USD

In this example, %.2f specifies that the floating-point number should be displayed with two decimal places.

Percent-encoding in URLs

When working with URLs, it’s important to encode certain characters to ensure they are treated correctly properly. This is where percent encoding comes into play. Percent-encoding is a mechanism for encoding data in a URI by replacing certain characters with a ‘%’ symbol followed by two hexadecimal digits representing the character’s ASCII code.

According to the URI specification (RFC 3986), URLs must encode characters like spaces, angle brackets, and other special characters as percent.

You can use Python’s urllib.parse module to handle URL encoding. Here’s an example:

from urllib.parse import quote

url = "https://pythonguides.com/search?q=New York"
encoded_url = quote(url)

print(encoded_url)

Output:

https%3A//pythonguides.com/search%3Fq%3DNew%20York

In this example, the quote() function from the urllib.parse module is used to percent-encode the URL, replacing spaces with %20 and other special characters with their encoded representations.

Conclusion

The percent sign (%) in Python is used for string formatting and interpolation. It allows you to insert values into string templates using placeholders and formatting specifiers. Additionally, percent-encoding is crucial when working with URLs to ensure special characters are properly handled.

I hope now you can use the percentage symbol in Python.

You may also like:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.