Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Python format() Function



The Python format() function returns the given value in the specified format based on the formatter.

The format() is one of the built-in functions and can be used for various purposes such as creating a well-formatted string, type-specific formatting, formatting numbers, string alignment and padding, etc.

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.

format() Function Examples

Practice the following examples to understand the use of format() function in Python:

Example: Use of format() Function

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: Convert Decimal to Binary, Octal, and Hexadecimal Formats

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: Override __format__ Class Method

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