Python – format()
Python format() built-in function is used to format a given value into a specified representation.
In this tutorial, we will learn the syntax and usage of format() built-in function, how to use this function to format values into a specific representation, with the help of example programs.
The syntax of format() function is
|format_spec||The format specification that effects the representation of the given value.|
Please refer Format Specification for different values that format_spec parameter can take, and how they effect the representation of value.
1. Format number with comma as thousand separator
In the following program, we take an integer value, and format this value with comma as a thousand separator.
2. Format decimal to scientific notation
In the following program, we take a decimal value, and format this value to scientific notation.
3. Format decimal to percentage
In the following program, we take a decimal value, and format this value to percentage notation. The decimal value is converted to percentage, and % symbol is appended to the value.
In this Built-in Functions tutorial, we learned the syntax of the format() built-in function, and how to use this function to format the representation of a given value.