In this tutorial , we will learn about python format() method and its uses.

What is python format() method?
The format() is a built-in python method that returns a formatted representation of the specified value.
The syntax of format() is:
format() Parameters
The format()methods takes single parameters but multiple arguments:
- values – one or more values that needed to be formatted.
format() Placeholders
The placeholders can be identified using names indexes {value}, numbered indexes like {0} , {1},etc. Or even empty {} placeholder.
Let’s check a simple example of the format()method.
Example 1: Simple formatting using format() method.
Output:
Formatting Specifiers
format() method also supports different types for formatting specifiers that helps to manipulate the results.
:< | Result will be aligned left |
:> | Result will be aligned right |
:^ | Result will be aligned center |
:= | Places the sign to the left most position |
:+ | Use a plus sign to indicate if the result is positive or negative |
:- | Use a minus sign for negative values only |
: | Use a space to insert an extra space before positive numbers (and a minus sign before negative numbers) |
:, | Use a comma as a thousand separator |
:_ | Use a underscore as a thousand separator |
:b | Binary format |
:c | Converts the value into the corresponding unicode character |
:d | Decimal format |
:e | Scientific format, with a lowercase e |
:E | Scientific format, with an uppercase E |
:f | Fix point number format |
:F | Fix point number format, in uppercase format (show inf and nan as INF and NAN) |
:g | General format |
:G | General format (using a upper case E for scientific notations) |
: o | Octal format |
: x | Hex format, lower case |
:X | Hex format, upper case |
:n | Number format |
:% | Percentage format |
Example 2: Using format specifiers with format() method.
Output:
Rules of format()
There are no such rules to follow in the format() method.