w3resource

Python: format() function

format() function

The format() function is used to format a specified value into a specified format.

Version:

(Python 3.2.5)

Syntax:

format(value[, format_spec])

Parameter:

Name Description Required /
Optional
value A value that will be formatted. Optional
format_spec Specification on how the value should be formatted. Optional

The general form of a standard format specifier is:

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"

Return value:

Example: Python () function

# x, f and b are type

# integer
print(format(25, "x"))

# float arguments
print(format(125.1234567, "f"))

# binary format
print(format(12, "b"))

Output:

19
125.123457
1100

Python Code Editor:

Previous: float()
Next: frozenset()

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.