×

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 slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute 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.

Python History

Python is one of the top trending, widely-used programming languages. Python is a general-purpose programming language. Python language is known for its features. Some features are given below: SimplisticFree and open...

4 minutes read.

Python program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

Programs for Printing Pyramid Patterns in Python

<!-- wp:paragraph --><p>Python supports printing patterns using basic for loops. The number of rows is handled by the first outer loop, while the number of columns is handled by the...

9 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

Python complex() class

Python complex() class The complex() class in Python returns a complex number or converts a string or number to a complex number. Syntax class complex([real[, imag]]) Parameter Real: This parameter consists of a number representing the real...

1 minute read.

Python str() function

Python str() function The str() function in Python converts the specified value into a string. Syntax class str(object='')           or class str(object=b'', encoding='utf-8', errors='strict') Parameter object:  This parameter represents any object to convert the given...

1 minute read.

Iterate a Dictionary in Python – Part 2

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

3 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to Program in Python on Raspberry pi?

Introduction to Python A popular programming tool with simple, complete novice syntax is Python structure of paragraphs, phrases, and words. Due to its widespread use, this has a large community that...

4 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 minutes read.

Python List Size

Introduction The list data type in Python is an ordered, flexible collection. A list may also contain duplicate entries. To get the size of any object, use the len() function in...

6 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Spyder (32-bit) - Free download

Spyder is a free and open-source scientific environment created by and for Python engineers, scientists, and data analysts. It is a robust scientific environment created in Python by and for...

3 minutes read.

Python datetime

Python provides a module named datetime to work with the date and time. Sometimes in the real life application development, we need to work with the date and time. The date is...

3 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.