Home  >  Article  >  Backend Development  >  How to use braces in .format in Python

How to use braces in .format in Python

anonymity
anonymityOriginal
2019-05-25 13:37:4910104browse

The format function of strings in python is very powerful. It can be said that it can completely replace other methods of using strings

How to use braces in .format in Python

Format function example:

The brace usage of format can realize the splicing of strings

The order within the braces can be scrambled, and the order in {} can be written into the format

Retain the decimal point of the output Number

Limit the number of digits in the output number, and also retain the number of decimal places

Thousands separator output

Percent sign output

How does Python use braces in .format

For example: the string hello {name}

'hello {name}.format(name='world')' Brackets are special escape characters. If you need original curly braces, use {{ instead of {, use }} instead of }, as follows:

>>> 'hello {{worlds in braces!}}, {name}'.format(name='zhangsan')
'hello {worlds in braces!}, zhangsan'

The above is the detailed content of How to use braces in .format in Python. 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