Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use f-string of python to format output

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to use f-string formatted output of python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction to f-string

Python3.6 introduces a new way of formatting strings: f-tring formatting strings. From% s formatting to format formatting to f-string formatting, formatting is becoming more and more intuitive, and f-string is more efficient and easier to use than the first two.

At the same time, it is worth noting that f-string is based on format formatting made some changes, the core idea of using the same as format, so you can learn% s and format formatting, and then learn f-string formatting.

2. Common ways of using f-string

2.1 basic use

① f-string indicates the field to be replaced in broad brackets {}, which can be filled directly with the replacement content.

> name = "Huang Wei"

> f "Hello, my name is {name}"

'Hello, my name is Huang Wei'

> num = 2

> > f "I have {num} apples"

'i have 2 apples'

> price = 95.5

> f "He has {price} $"

'He has 95.50 $'

2.2 expression evaluation and function calls

The curly braces {} of ① f-string can be filled in an expression or a calling function, and Python will find the result and fill it in the returned string.

> f "They have {2-5-2} apples"

'They have 12 apples'

> name = "Huang Wei"

> f "my name is {name.lower ()}"

'my name is huang wei'

> import math

> f "Pi is {math.pi}"

The value of Pi is 3.141592653589793'.

Using lambda anonymous functions in ② f-string: complex numerical calculations can be done

> aa = 123.456

> f "{(lambda x:x*5-2) (aa): .2f}"

'615.28'

> bb = 8

> cc = 2

> f "{(lambda x ~ (10) y) (bb,cc)}"

'10'

Note: pay attention to the syntax format, the first parenthesis represents the lambda expression, the second parenthesis indicates the input parameters to the lambda expression.

2.3 problems in the use of quotation marks in f-string

The quotation marks used in ① f-string large braces cannot conflict with the quotation marks outside the curly braces. It is necessary to flexibly change the use of single quotation marks, double quotation marks, single triple quotation marks and double triple quotation marks according to the situation.

> Fairi am {"Huang Wei"}'

'I am Huang Wei'

> favored I am {'Huang Wei'}''

'I am Huang Wei'

> f "I am {'Huang Wei'}"

'I am Huang Wei'

> f "I am {" Huang Wei "}"

'I am Huang Wei'

> Fairi am {'Huang Wei'}'

File "", line 1

Fairi am {'Huang Wei'}'

^

SyntaxError: invalid syntax

Note: as long as the quotation marks inside and outside the curly braces are different, there is no problem. However, there can only be quotation marks and double quotation marks in curly braces, and quotation marks outside curly braces can use single quotation marks, double quotation marks, single triple quotation marks, double triple quotation marks.

Quotation marks outside ② curly braces can also be escaped using\, but not inside curly braces.

> f "he\'ll go to {'shang hai'}"

"he'll go to shang hai"

> f "he introduces himself {" I\ m Tom "}"

File "", line 1

SyntaxError: f-string expression part cannot include a backslash

> f "he introduces himself {" Isimm Tom "}"

"he introduces himself I'm Tom"

2.4 problems in the use of braces in f-string

If you need to display curly braces outside ① f-string braces, enter two consecutive curly braces {{}}; quotation marks are needed inside the curly braces, just use quotation marks.

> f "5 {'{apples}'}"

'5 {apples}'

> f "{{5}} {'apples'}"

'{5} apples'

2.5 f-string fill

① what is padding?

   concept: when we specify the final length of a string, if the existing string is not that long, then we fill this length with some character (padding character), which is called "padding".

The ② demo code is as follows: blank padding is used by default

> name = "Huang Wei"

> f "{name: > 20}"

'Huang Wei'

> > f "{name:20}"

'_ Huang Wei'

> > f "{name:_10.3}"

'_ Hel'

> f "{aVue _

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report