Python format() Function



The Python format() function is a built-in function that converts a given value into a formatted representation according to the passed format specifier.

This function can be used to format multiple types of data in different ways, such as adding spaces between strings, and formatting numbers, dates, and times.

Syntax

Following is the syntax of the Python format() function −

format(value, formatSpec = '')

Parameters

The Python format() function accepts the following parameters −

  • value − It represents a value (number or string) that needs to be formatted.

  • formatSpec − It represents a format that specifies how the value has to be formatted. Its default value is an empty string.

Return Value

The Python format() function returns a formatted representation of the passed value according to the specified format.

Examples

In this section, we will see some examples of format() function −

Example 1

The following example shows how to use the Python format() function to format the given number into a specific form. Here, this function is applied to multiple numbers to format them into different forms.

numOne = 5100200300
formattedNum = format(numOne, ",") 
print("Formatting the number using separator:", formattedNum)
   
numTwo = 12.756
formattedNum = format(numTwo, ".2%")
print("Rounding the given number:",formattedNum)
   
numThree = 500300200
formattedNum = format(numThree, "e")
print("Converting number into exponential notation:", formattedNum)
   
numFour = 541.58786
formattedNum = format(numFour, ".2f")
print("Formatting number to two decimal places:", formattedNum)  

When we run the above program, it produces the following result −

Formatting the number using separator: 5,100,200,300
Rounding the given number: 1275.60%
Converting number into exponential notation: 5.003002e+08
Formatting number to two decimal places: 541.59

Example 2

The Python format() function can be used to convert a given number into its corresponding binary, octal and Hexadecimal representation as shown in the below example.

nums = 124
binaryNum = format(nums, "b")
octalNum = format(nums, "o")
hexNum = format(nums, "x")
print("Converting number into Binary:", binaryNum)  
print("Converting number into Octal:", octalNum)  
print("Converting number into Hexadecimal:", hexNum)  

When we run the above program, it produces following result −

Converting number into Binary: 1111100
Converting number into Octal: 174
Converting number into Hexadecimal: 7c

Example 3

By overriding the "__format__" method in a class, we can customize how objects of that class should be formatted. The code below demonstrates the same.

import datetime

class DefDate:
   def __init__(self, year, month, day):
      self.date = datetime.date(year, month, day)

   def __format__(self, formatSpec):
      return self.date.strftime(formatSpec)

formattedDate = DefDate(2024, 4, 17)
print("Date after formatting:")
print(format(formattedDate, "%B %d, %Y"))

Output of the above code is as follows −

Date after formatting:
April 17, 2024
python_built_in_functions.htm
Advertisements