×

Python Printf Style Formating

String objects have one special implicit activity: the % administrator (modulo). This is otherwise called the string designing or interjection administrator. Given design % values (where configuration is a string), % transformation determinations in design are supplanted with nothing or more components of values. The impact is like utilizing the sprintf() in the C language.

On the off chance that configuration requires a solitary contention, values might be a solitary non-tuple object. 5 If not, values should be a tuple with the very number of things indicated by the configuration string, or a solitary planning object (for instance, a word reference).

A transformation specifier contains at least two characters and has the accompanying parts, which should happen in a specific order:

  1. The '%' character denotes the beginning of the specifier.
  2. Mapping key (discretionary), comprising of a parenthesized grouping of characters (for instance, (somename)).
  3. Conversion banners (discretionary), which influence the aftereffect of some transformation types.
  4. Minimum field width (discretionary). Whenever determined as an '*' (reference bullet), the genuine width is perused from the following component of the tuple in values, and the item to change overcomes after the base field width and discretionary accuracy.
  5. Precision (discretionary), given as a '.' (speck) trailed by accuracy. Whenever determined as '*' (a mark), the genuine accuracy is perused from the following component of the tuple in values, and the worth to change is overcome after the accuracy.
  6. Length modifier (discretionary).
  7. Conversion sort.

The change types are:

  • 'd'- >Signed whole number decimal.
  • 'I'- >Signed whole number decimal.
  • 'o'- >Signed octal worth.
  • 'u'- >Obsolete type - it is indistinguishable from 'd'.
  • 'x'- >Signed hexadecimal (lowercase).
  • 'X'- >Signed hexadecimal (capitalized).
  • 'e'- >Floating point outstanding organization (lowercase).
  • 'E'- >Floating point dramatic configuration (capitalized).
  • 'f'- >Floating point decimal arrangement.
  • 'F'- >Floating point decimal organization.
  • 'g'- >Floating point design. Utilizes lowercase remarkable arrangement on the off chance that example is not exactly - 4 or at the very least accuracy, a decimal configuration in any case.
  • 'G'- >Floating point design. Utilizes capitalized outstanding arrangement on the off chance that example is not exactly - 4 or at least accuracy, a decimal configuration in any case.
  • 'c'- >Single character (acknowledges whole number or single person string).
  • 'r'- >String (changes over any Python object utilizing repr()).
  • 's'- >String (changes over any Python object utilizing str()).
  • 'a'- >String (changes over any Python object utilizing ASCII()).
  • '%'- >No contention is changed over, resulting in a '%' character in the outcome.

Notes:

  • The other structure causes a main octal specifier ('0o') to be embedded before the primary digit.
  • The other structure causes a main '0x' or '0X' (contingent upon whether the 'x' or 'X' design was utilized) to be embedded before the principal digit.
  • The other structure makes the outcome generally contain a decimal point, regardless of whether no digits follow it.
  • The accuracy decides the number of digits after the decimal point and defaults to 6.

Related Topics

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered. Following...

3 minutes read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Cx_Oracle Python with Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Python Set discard() method

Python Set discard() method The set.discard() method in Python removes a specified element from the set (if present). Syntax set.discard(value) Parameter value- This parameter represents the element to be removed from the set. Return None Example 1 # Python program explaining # the...

1 minute read.

Python program for perfect number

Python program for perfect number Before writing any program for a given problem, we have to understand the problem for which we are creating a solution program. So, let's understand what...

2 minutes read.

List Iteration in Python

In this tutorial, we will learn how to iterate list in Python. List in Python A list is an ordered group of values which includes several kinds of values.A list is a mutable...

3 minutes read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

Python Glob

Methods for matching files that include particular patterns in accordance with UNIX shell-related expansion rules are referred to as "glob" methods. Similar methods for finding, locating, and searching for all of...

12 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Iterate a Dictionary in Python

In this tutorial, we will learn how to Iterate a Dictionary in Python. Dictionary: In Python, a dictionary is an unordered collection of data values that is used to store data values,...

3 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Python map() function

Python map() function The map() function in Python returns an iterator that applies a function to every item of iterable, yielding the results. Syntax map(function, iterable, ...) Parameter function: It is a required parameter that represents the function to...

1 minute read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

What is the Python Global Interpreter Lock?

Introduction When working with processes, Python employs a form of process lock called the Global Interpreter Lock (GIL). Python typically executes a collection of typed statements using just one thread. It...

4 minutes read.

Shallow Copy and Deep Copy in Python

Shallow Copy and Deep Copy in Python In this section, we will learn about the Shallow Copy and Deep Copy in the Python program. But before going through the topic, we...

6 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python min() function

Python min() function returns you the minimum value element in an iterable. We can also use this function to determine the smallest value among various arguments passed to this function. We...

4 minutes read.

Python eval() function

Python eval() function The eval() function in Python is used to evaluate the given expression. If the specified expression is a legal Python statement, it will be executed. Syntax eval(expression, globals=None, locals=None) Parameter expression: This parameter represents a String,...

1 minute read.

List Assignment Index out of Range in Python

As we know, a List is one of the four unique data structures available in Python; In this tutorial, we will deep dive into understanding iterating through a list and...

3 minutes read.