Home  >  Article  >  Backend Development  >  What does format in python mean?

What does format in python mean?

silencement
silencementOriginal
2019-06-24 17:47:5242164browse

What does format in python mean?

format function

This is a string formatting method, such as str.format().

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

The following shows two main usages:

(1) For example: the statement print("{:.2f}".format(3.1415926)), its output is 3.14, as can be seen The command is to preserve two decimal points.

(2) For example: the statement "{1} {0} {1}".format("hello", "world"), its output is 'world hello world', it can be seen that the format is They set the location.

Another example: the statement print '{} *'.format(j) indicates the value of the output variable j and the multiplication sign: j*. It can be seen that when used in a loop, a multiplication equation can be output.

Another example: The statement print "{} corresponds to the position {{0}}".format("runoob"), and its output is 'runoob The corresponding position is 0'.

The following table shows the various ways str.format() formats numbers:


The above is the detailed content of What does format in python mean?. 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