×

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

apply() function in python

Pandas: Pandas is a library in python. It is an open source package in python. Pandas in python are used for data cleaning and data analysis. Data frame mainly consists of...

3 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

How to Fix an EOF Error in Python?

Introduction An EOF (End-of-File) error occurs when a program tries to read beyond the end of a file or a stream, causing it to return an error message. It can happen...

8 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 String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

5 minutes read.

Find Last Occurrence of Substring using Python

Introduction When planning to work with strings, we may need to determine whether a substring is present. This issue is rather typical, and there have been numerous discussions about how to...

3 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Python Dictionary clear() method

Python Dictionary clear() method The dictionary.clear() method in Python removes all the elements from a dictionary. Syntax dictionary.clear() Parameter NA Return None Example 1 # Python program explaining # the dictionary.clear() method # initialising the dictionary fruits =...

1 minute read.

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix...

3 minutes read.

Compound Interest GUI Calculator using PyQt5 in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

Text detection 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...

4 minutes read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

One Liner If-Else Statements in Python

Before learning one liner if else Python, let us first know what Python is. Python is a broadly helpful, object-oriented and dependable language having a large number of utilizations from...

4 minutes read.