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 does Python format a string

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to format strings in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to format strings by Python".

Use comparison

Let's first take a look at the comparison of the use of these formatted strings that already exist in Python.

#% susername = 'tom'action =' payment'message = 'User% s has logged in and did an action% s.'% (username, action) print (message) # formatusername =' tom'action = 'payment'message =' User {} has logged in and did an action {}. 'format (username Action) print (message) # f-stringusername = 'tom'action =' payment'message = f'User {user} has logged in and did an action {action}. 'print (message) f "{2 * 3}" # 6comedian = {' name': 'Tom',' age': 20} f "The comedian is {comedian ['name']}, aged {comedian [' age']}." # 'The comedian is Tom, aged 20.'

Compared to the common string formatter% s or format methods, f-strings inserts variables directly into placeholders is more convenient and easier to understand.

Convenient converter

F-string is currently the best form of concatenating strings, with more powerful features, let's take a look at the structure of f-string.

F'{}.'

Where'! s' calls str () on the expression,'! r 'calls repr () on the expression, and'! a 'calls ascii () on the expression.

By default, f-string will use str (), but if you include the conversion flag! r, you can use repr ()

Class Person: def _ init__ (self, name, age): self.name = name self.age = age def _ str__ (self): return f'str-name: {self.name}, age: {self.age} 'def _ repr__ (self): return f'repr-name: {self.name}, age: {self.age}' p = Person ('tom', 20) f' {p}'# str-name: tom Age: 20f'{pawr}'# repr-name: tom, age: 20

Conversion flag! a

A ='a string'f' {Ayoga}'# "'a string'"

Equivalent to

F'{repr (a)}'# "a string'"

Performance

In addition to providing powerful formatting capabilities, f-string is also the highest performance implementation of the three formatting methods.

> import timeit > timeit.timeit ("name =" Eric "... Age = 74. % s is% s.% (name, age) "", number = 10000) 0.003324444866599663 > timeit.timeit ("name =" Eric ". Age = 74. '{} is {}.' .format (name, age) "", number = 10000) 0.004242089427570761 > timeit.timeit ("name =" Eric ". Age = 74. F'{name} is {age}. "", number = 10000) 0.0024820892040722242. I believe you have a better understanding of "how to format strings in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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