×

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand.

Example:

value1 = 8
value2 = 2
remainder = value1 % value2
print(remainder)

Output:

What Does the Percent Sign (%) Mean in Python?

Subsequent to composing the above code (what does the per cent sign mean in python), Ones you will print the " leftover portion ", then, at that point, the result will show up as a " 0 ". Here, the modulo operator " % " returns the rest of isolating the two numbers.

The % operator advises the Python mediator to design a string utilizing a given arrangement of factors encased in a tuple, following the administrator. An exceptionally basic illustration of this is the following:

'%s is smaller than %s' % ('one', 'two')

The Python translator substitutes the main event of %s in the string by the given string "one" and the second %s by the string "two". These %s strings are really placeholders in our "layout" string, and they show that strings will be put there.

As a first model, beneath, we show utilizing the Python REPL how to print string esteem and float esteem:

print("Mr. %s, the total is %.2f." % ("Jekyll", 15.53))

Output:

What Does the Percent Sign (%) Mean in Python?

Very much like the %s is a placeholder for strings, %f is a placeholder for drifting point numbers. The ".2" preceding the f shows the number of digits we that need showed after the decimal point.

These are only two basic instances of what is conceivable, and much more placeholder types are upheld. Here is the full rundown of placeholder types in more detail:

%c

This placeholder addresses a solitary person.

print("The character after %c is %c." % ("A", "C"))

Output:

What Does the Percent Sign (%) Mean in Python?

Giving in excess of a solitary person as the variable here will raise an exemption.

%s

This placeholder utilizes string change through str() preceding designing. So any worth that can be changed over completely to a string through str() can be utilized here.

place = "India"
print("Welcome to %s!" % place)

Output:

What Does the Percent Sign (%) Mean in Python?

Here we just have a solitary component to be utilized in our string designing, and in this way, we're not expected to encase the component in a tuple like the past models.

%I and %d

These placholders address a marked decimal whole number.

y = 2019
print("%i will be a perfect year." % y)

Output:

What Does the Percent Sign (%) Mean in Python?

Since this placeholder expects a decimal, it will be changed over completely to one in the event that drifting point esteem is given all things being equal.

%u

This placeholder addresses an unsigned decimal whole number.

%o

This placeholder addresses an octal whole number.

num = 15
print("%i in octal is %o" % (num, num))

Output:

What Does the Percent Sign (%) Mean in Python?

%x

Addresses a whole hexadecimal number utilizing lowercase letters (a-f).

number = 15
print("%i in hex is %02x" % (number, number))

Output:

What Does the Percent Sign (%) Mean in Python?

By utilizing the "02" prefix in our placeholder, we're advising Python to print a two-character hex string.

%X

Addresses a whole hexadecimal number utilizing capitalized letters (A-F).

number = 15
print("%i in hex is %04X" % (number, number))

Output:

What Does the Percent Sign (%) Mean in Python?

What's more, similar to the past model, by utilizing the "04" prefix in our placeholder, we're advising Python to print a four-character hex string.

%e

Addresses dramatic documentation with a lowercase "e".

%E

Addresses dramatic documentation with a capitalized "e".

%f

Addresses a drifting point genuine number.

pr= 15.95
print("the price is %.2f" % pr)

Output:

What Does the Percent Sign (%) Mean in Python?

%g

The more limited adaptation of %f and %e.

%G

The more limited adaptation of %f and %E.

The placeholders displayed above permit you to arrange strings by determining information types in your formats. Be that as it may, these aren't the main elements of the addition administrator. In the following subsection, we'll perceive the way we can cushion our strings with spaces utilizing the % administrator.

Adjusting the Output

As of recently, we've just been told the best way to arrange text strings is by indicating basic placeholders. With the assistance of extra mathematical worth, you can characterize the all-out space that will be held on one or the other side of a variable in the result string.

As an illustration, the worth of %10s saves ten characters, with the additional separating on the left half of the placeholder, and the worth of %-10s puts any additional room to the right of the placeholder. The single cushioning character is a space and can't be changed.

place = "London"
print ("%10s is not a place in France" % place)  # Pad to the left

Output:

What Does the Percent Sign (%) Mean in Python?
place = "London"
print ("%-10s is not a place in France" % place) # Pad to the right

Output:

What Does the Percent Sign (%) Mean in Python?

Managing numbers works similarly:

print ("The postcode is %10d." % 25000)    # Padding on the left side

Output:

What Does the Percent Sign (%) Mean in Python?
print ("The postcode is %-10d." % 25000)   # Padding on the right side

Output:

What Does the Percent Sign (%) Mean in Python?

Related Topics

Math Module in Python

In this article, you are going to learn everything in detail about the “ math “ module in Python. We can normally work with general operations using python without importing or...

16 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.

Python Matrix Operations

Python Matrix In this section of the Python tutorial, we will look at the introduction of the matrix in Python programming. We will perform many operations on the Python matrix using...

21 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

Artificial intelligence mini projects with source code in Python

Project Name: Movie recommendation system A recommendation provides customers with relevant information related to their searches. Before the recommendation system, the most common method of purchasing was to rely on the...

4 minutes read.

Enumerate() Function in Python

The enumerate() function in Python is used to convert a data collection object into an enumerate object. It returns an object that contains a counter as a key for each...

3 minutes read.

Python - Binomial Distribution

Introduction Definition of the Binomial Distribution The method of counting how many instances of a specific event there have been is called the binomial distribution. It will outline the possible outcomes or...

4 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

Python abs() function

Python abs() function The abs() function returns the absolute value of a number. Syntax abs(x) Parameter x: The parameter ‘x’ can be an integer value, a floating point number or a complex number. Return This function returns...

1 minute read.

Python id() function

Python id() function The id() function in Python returns an id for the specified object where all the objects in has its own unique id. Syntax id(object) Parameter object: This parameter represents any object, String, Number, List,...

1 minute read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Python Escape Characters

In this tutorial, we will learn how to use the Escape Characters in Python. Escape Character: Escape Characters are used for some special meaning in our statements. It is denoted or represented...

3 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Comments in the Python Programming Language

In this tutorial, we will learn how to comment in multiple lines and paragraphs in the python programming language. Commenting a Paragraph in Python Most people think the importance of comments is...

2 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Difference between Yield and Return in Python

Python yield statement The generators are defined by using the yield statement in Python. Generally, it converts a normal Python function into a generator.  The yield statement hauls the function and returns...

3 minutes read.