×

Python Percentage Sign

In Python, the percentage sign significantly completes two things. They are:

  1. It goes about as a Modulo administrator.
  2. It helps in string organizing.

Allow us to see every one of them plainly.

Modulo operator:

Like the addition (+), subtraction(- ), multiplication (*) and division (/), the modulo (%) is additionally a math administrator which assesses the rest of the division between two operands. These operands can be any number or float.

Language syntax: a % b

Allow us to assume if the modulo administrator is applied between two numbers an and b i.ea%b, it computes the rest of partitioning the main number a with the second number b.

Program:

a = 11
b = 2
c = a % b
print(c)

Output:

1

String organizing utilizing % percentage sign:

Like the other programming dialects like C, the % rate sign can be utilized to organize the string. Consequently, it is otherwise called the string designing administrator. A portion of the placeholders in Python are:

  • %d - for whole number
  • %f - for float
  • %s - for string
  • %o - for octal number
  • %x - for hexadecimal number

We really want to involve these placeholders in the string so the ideal variable replaces them in the subsequent string as for the information kind of factor.

The factors are should introduce following a string followed by the % rate sign.

Program:

a='Hello'
b=25
c=3.21
print("The worth of an is %s"%a)
print("The worth of b is %d"%b)
print("The worth of c is %f"%c)
print("The hexadecimal worth of b is 0x%x"%b)
print("The Octal worth of b is %o"%b)

Output:

The worth of an is Hi
The worth of b is 25
The worth of c is 3.210000
The hexadecimal worth of b is 0x19
The Octal worth of b is 31
  • From the above program, we can see that the factors of type String, Number and Float supplanted the placeholders %s, %d and %f in the string separately.
  • Likewise, the number variable b changed over into hexadecimal and octal numbers utilizing the %x and %o placeholders
  • If the string comprises of beyond what one organization specifier, we can remember the variable names for a tuple.

Model:

name='Ravi'
age=25
print("My name is %s and my age is %d" %(name,age))

Output:

I go by Ravi and my age is 25
  • Here, the factors should follow the request for their placeholders. In any case, the translator raises TypeError.
  • Now and again, to address the rates, we might have to print the rate sign followed by a whole number. By then, the % rate sign is utilized two times to get away.

Model:

result = "Ravi got 95"
print("%s%%" %result)

Output:

Ravi got 95%

Related Topics

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.

How to convert integer to float in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

5 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python property()

Python property() class The property() class in Python returns a property attribute. Syntax class property(fget=None, fset=None, fdel=None, doc=None) Parameter fget: This attribute is used for getting an attribute value. fset : This parameter sets an attribute value. fdel: It is a function for deleting...

1 minute read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute 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 Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python Variables

Variables are one of the most important terms we should be familiar with if we want to be good programmers. In simpler words, we use variables to store a value....

4 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.

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

How To Compare Two Strings In Python

In this article, we will discuss how to compare two strings in Python. So, before that, let’s have a quick revision on “What are strings?” Strings are a sequence of characters that...

5 minutes read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Python Encapsulation

What is meant by Encapsulation? The process of restricting access to the methods and variables so that accidental modification of data can be prevented is known as Encapsulation. The motive of Encapsulation:...

3 minutes read.