Format () function in Python
The format () function is built into Python to use to format an input value into a specific format.
Syntax of format () function in Python:
format(value[, format_spec])
Parameters of format function ()
The format () function has 2 parameters:
value
: value to be formatted.format_spec
: the format you want for the value.
Format sets:
The order format is as follows:
[[fill]align][sign][#][0][width][,][.precision][type]
Inside:
- fill: is any character
- align: alignment, "<" | ">" | "=" | "^"
- sign: sign, "+" | "-" | ""
- width: string length, is an integer
- precision: precision, is an integer
- type: format type, "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Value returned from format ()
The format () method returns the formatted result of a given value determined by a specific format.
Example 1: Number format with format ()
# d, f và b là loại định dạng # số nguyên print(format(123, "d")) # số thập phân # viết bởi TipsMake.com print(format(123.4567898, "f")) # nhị phân print(format(12, "b"))
Run the program, the result is:
123 123.456790 1100
Example 2: Number format with alignment
# số nguyên print(format(1234, "*>+7,d")) # số thập phân print(format(123.4567, "^-09.3f"))
Output returns:
*+1,234 0123.4570
Here, when formatting the integer 1234, we specify the format including *<+7,d
. Why is that? Details are as follows:
*
Character fill, fill in the blank after formatting.>
Right alignment option, so the output string is on the right.+
Option to force the output to be signed (with the sign on the left).7
Optionally specify the length of the result, forcing the return number to have a minimum length of 7 characters, other spaces will be filled with fill characters.,
Thousands operators, commas will be placed between thousands of digits and the remaining digits.d
option to specify the result is an integer.
When formatting floating point numbers 123.4567, we specify the format including ^-09.3f
, details are as follows:
^
option to align the center position, so the output sequence will be in the middle of the specified space.-
Option to force the output to display a negative sign.0
Characters added to the spaces after formatting.9
Optionally specify the length of the result, forcing the return number to have a minimum length of 9 characters (including decimal points, commas, and a negative sign)..3
precision operator, determine the precision, round to the third digit after the decimal point.f
option to specify the result to return is a floating-point number.
Example 3: Use format () by overwriting __format __ ()
# phương thức __format __ () tùy chỉnh class Person: def __format__(self, format): if(format == 'age'): return '23' return 'None' print(format(Person(), "age"))
Run the program, the result is:
23
Here, we override the __format __ () method of the Person class, and the format () method inside of running Person().__format__("age")
to return 23
.
See also: Built-in Python functions
You should read it
- Max () function in Python
- Zip () function in Python
- The function set () in Python
- Int () function in Python
- The function dir () in Python
- The map () function in Python
- Help () function in Python
- The oct () function in Python
- The next () function in Python
- The ord () function in Python
- Sum () function in Python
- The function id () in Python
Maybe you are interested
Excel 2016 - Lesson 12: Formatting pages and printing spreadsheets in Excel
How to turn off auto formatting in Excel
Instructions for changing and editing music file information using 3uTools
Tips for formatting cells in Excel for professional spreadsheets
Detecting a new ransomware strain that specializes in stealing login information from the Chrome browser
How to fix the problem of Word's numbered lists having annoying formatting