Python Built-in Function format():

The format() is a built-in function used to convert a given value/object into the specified format. This method supports complex variable substitutions and value formatting. Moreover, this function will raise an TypeError exception, if the format specifier is invalid.

Signature

The signature for the format() function is as shown below. Here, value is an object and format_spec is a format specifier.

s=format(value,format_spec)

Parameters and return type

  • Here, value is an object to format. However, format_spec specifies the format of the object. It can take any of the values given below.
    • ‘<‘ – Left aligns the result
    • ‘>’ – Right aligns the result
    • ‘^’ – Center aligns the result
    • ‘ ‘ – Use a leading space for positive numbers
    • ‘=’ – Places the sign to the leftmost position
    • ‘+’ – Use a plus sign to indicate if the result is positive or negative
    • ‘-‘ – Use a minus sign for negative values only
    • ‘b’ – Binary format
    • ‘c’ – Converts the value into the corresponding Unicode character
    • ‘d’ – Decimal format
    • ‘e’ – Scientific format, with a lower case e
    • ‘E’ – Scientific format, with an upper case E
    • ‘,’ – Use a comma as a thousands separator
    • ‘_’ – Use an underscore as a thousand separator
    • ‘f’ – Fix point number format
    • ‘F’ – Fix point number format, upper case
    • ‘o’ – Octal format
    • ‘x’ – Hex format, lower case
    • ‘X’ – Hex format, upper case
    • ‘n’ – Number format
    • ‘%’ – Percentage format
  • However, it returns the formatted value of a given parameter.

Python Built-in Function format Examples:

Example 1: In this example, let us take an integer number and a float number. Thereafter, we will format it using a comma and % format specification. Thus, we can see integer number used comma as a thousand separator and float number is converted to percentage format.

#Initializing 
a = 3552491
b=0.75
#using format function and printing
result = format(a, ',')
print(result)
result = format(b, '%')
print(result)

Output

3,552,491
75.000000%

Example 2: In this case, let us demonstrate the working of format() function with a string. Firstly, we will take string to format in a variable s. As a result,  we can see {} will be replaced by the parameters of the format function.

#Initializing 
s="We are {} of our {} {}"
#using format function and printing
r = s.format("proud","country","India")
print(r)

Output

We are proud of our country India

Example 3: In this example, let us convert an integer number to an octal number, a Hexadecimal number and a binary number. Thereafter, we will write an unknown format specifier. As a result, it raises a ValueError exception.

#Initializing 
a=10
#Using format and printing
print("Octal format of 10 is ",format(a,'o'))
print("Hex format of 10 is ",format(a,'x'))
print("Binary format of 10 is ",format(a,'b'))
print("Binary format of 10 is ",format(a,'cc'))

Output

Octal format of 10 is  12
Hex format of 10 is  a
Binary format of 10 is  1010
Traceback (most recent call last):
  File "main.py", line 6, in 
    print("Binary format of 10 is ",format(a,'cc'))
ValueError: Invalid format specifier

Conclusion

The format() function returns the formatted object based on the given format specifier. However, it raises an exception, if an invalid format specifier is given.

References

Happy Learning 🙂

Leave A Comment