Home >Backend Development >Python Tutorial >Python uses the format method to retain three decimal places

Python uses the format method to retain three decimal places

王林
王林Original
2020-05-10 13:49:4118830browse

Python uses the format method to retain three decimal places

# The format method is a built-in Python string formatting method.

The basic syntax is: str.format(), which enhances the string formatting function.

The basic syntax is to use {} and : to replace the previous %.

The format function can receive multiple parameters, and the positions do not need to be in order.

Specific examples are as follows:

>>> print("{:.0f}".format(3.1415926))
3
>>> print("{:.2f}".format(3.1415926))
3.14
>>> print("{:.3f}".format(3.1415926))
3.142

Recommended tutorial: python tutorial

The above is the detailed content of Python uses the format method to retain three decimal places. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn