Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python format() Function



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

Theformat() is one of thebuilt-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 Pythonformat() function −

format(value, formatSpec = '')

Parameters

The Pythonformat() function accepts the following parameters −

  • value − It represents a value (number orstring) 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 Pythonformat() 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 offormat() 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 = 5100200300formattedNum = format(numOne, ",") print("Formatting the number using separator:", formattedNum)   numTwo = 12.756formattedNum = format(numTwo, ".2%")print("Rounding the given number:",formattedNum)   numThree = 500300200formattedNum = format(numThree, "e")print("Converting number into exponential notation:", formattedNum)   numFour = 541.58786formattedNum = 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,300Rounding the given number: 1275.60%Converting number into exponential notation: 5.003002e+08Formatting 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 = 124binaryNum = 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: 1111100Converting number into Octal: 174Converting 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 datetimeclass 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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp