Trusted answers to developer questions

How to use format() function in Python

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

The format() function is a built-in function in Python that returns a formatted representation of a value controlled by a format specifier. This method is created to handle complex string formatting more efficiently.

Returns: It returns the formatted representation of the value controlled by the format specifier.

Syntax

format(valuee[, format_specifier])

From the above syntax, we can say that the format() function takes two parameters, value (the value that needs to be formatted) and format_specifier (the specification of how the value needs to be formatted).

Code

print(format(255.4226,"f")) #float argument
print(format(365,"d")) #integer
print(format(45,"b")) #binary argument

RELATED TAGS

python
Did you find this helpful?