Another useful Type Specifying

  • %u unsigned decimal integer
  • %o octal integer
  • f – floating-point display
  • b – binary number
  • o – octal number
  • %x – hexadecimal with lowercase letters after 9
  • %X– hexadecimal with uppercase letters after 9
  • e – exponent notation

You can also specify formatting symbols. The only change is using a colon (:) instead of %.

For example, instead of %s use {:s} and instead of %d use (:d}

Syntax : String {field_name:conversion} Example.format(value)
Errors and Exceptions : 
ValueError : Error occurs during type conversion in this method. 

Convert base-10 decimal integers to floating-point numeric constants 

Python3




print("This site is {0:f}% securely {1}!!".
      format(100, "encrypted"))
 
# To limit the precision
print("My average of this {0} was {1:.2f}%"
      .format("semester", 78.234876))
 
# For no decimal places
print("My average of this {0} was {1:.0f}%"
      .format("semester", 78.234876))
 
# Convert an integer to its binary or
# with other different converted bases.
print("The {0} of 100 is {1:b}"
      .format("binary", 100))
 
print("The {0} of 100 is {1:o}"
      .format("octal", 100))


Output

This site is 100.000000% securely encrypted!!
My average of this semester was 78.23%
My average of this semester was 78%
The binary of 100 is 1100100
The octal of 100 is 144

Python String format() Method

The format() method is a powerful tool that allows developers to create formatted strings by embedding variables and values into placeholders within a template string. This method offers a flexible and versatile way to construct textual output for a wide range of applications. Python string format() function has been introduced for handling complex string formatting more efficiently. Sometimes we want to make generalized print statements in that case instead of writing print statements every time we use the concept of formatting.

Similar Reads

Python String Format() Syntax

Syntax: { }.format(value) Parameters:  value : Can be an integer, floating point numeric constant, string, characters or even variables. Returntype: Returns a formatted string with the value passed as parameter in the placeholder position....

String Format() in Python Example

A simple demonstration of Python String format() Method in Python....

String format() with multiple placeholders

...

Formatting Strings using Escape Sequences

...

Formatters with Positional and Keyword Arguments

...

Type Specifying In Python

Multiple pairs of curly braces can be used while formatting the string in Python. Let’s say another variable substitution is needed in the sentence, which can be done by adding a second pair of curly braces and passing a second value into the method. Python will replace the placeholders with values in order....

Another useful Type Specifying

...

Type Specifying Errors

...

Padding Substitutions or Generating Spaces

You can use two or more specially designated characters within a string to format a string or perform a command. These characters are called escape sequences. An Escape sequence in Python starts with a backslash (\). For example, \n is an escape sequence in which the common meaning of the letter n is literally escaped and given an alternative meaning – a new line....

Applications

When placeholders { } are empty, Python will replace the values passed through str.format() in order. The values that exist within the str.format() method are essentially tuple data types and each individual value contained in the tuple can be called by its index number, which starts with the index number 0. These index numbers can be passed into the curly braces that serve as the placeholders in the original string....

Using a dictionary for string formatting

...

Python format() with list

More parameters can be included within the curly braces of our syntax. Use the format code syntax {field_name: conversion}, where field_name specifies the index number of the argument to the str.format() method, and conversion refers to the conversion code of the data type....