current position:Home>Learn Python systematically - string (str): string formatting expression - [basic knowledge]

Learn Python systematically - string (str): string formatting expression - [basic knowledge]

2022-09-09 03:34:45von Neumann

Categories: "System Learning Python" General Catalog

Related Articles:
String Formatting Expressions: The Basics
String Formatting Expressions: Formatting Expressions
String Formatting Expressions: Formatting Methods


Python also provides an advanced way to combine string processing tasks. String formatting allows multiple types of substitutions to be performed on a string in a single step.Strictly speaking, it is not required, but it is convenient to use, especially when formatted text is displayed to the user of the program.Since the Python world is full of new ideas, string formatting in Python today can be implemented in two forms:

  • String formatting expression: '...%s... % (values)': This is the original technology since the birth of Python, this form isC-based printf model and widely used in most existing code.
  • String formatting method call: '...{}...'.format(values): This is a new technology added in Python2.6 and Python3.0, this formPartially derived from the C#/.NET tool of the same name, and has a lot of overlap with the functionality of string formatting expressions.

Because of the newer form of method invocation, some or other of these may be deprecated or removed over time.When Python 3.0 was released in 2008, expressions seemed more likely to be deprecated in later Python versions.Indeed, the 3.0 documentation threatens to deprecate expressions in 3.1 and remove them after that.As of 2013 and Python 3.3, this didn't happen, and given the widespread use of expressions, it doesn't look like it will happen now - in fact, it's still present in Python's own standard library todayA thousand times!Naturally, the development of this story depends on the future practices of Python users.On the other hand, because both expressions and methods are valid today, and both may appear in code you stumble across, this article covers both techniques in full.As you'll see, the two are largely a variation on the theme, although methods have some extra features (such as thousands separators), and expressions are often more concise, and for most Python programmers, expressionsmore like their habits.For illustrative purposes, the text uses both techniques in the examples that follow.If its author has a preference, he will generally remain categorized, except to quote from Python's import this motto: there should only be a single obvious solution.

In the original and long-standing sense of the phrase, unless the newer methods of string formatting are overwhelmingly better than the original and widely used expressions, it will be very important to Python programmers in thisThe requirement to double the domain's knowledge base is unfounded, not even Pythonic enough.If two complex tools roughly overlap, programmers shouldn't learn them at the same time.It's up to you to judge whether formatting is enough to add weight to the language, so let's listen to the stories of both in peace.

References:
[1] Mark Lutz. Python Learning Manual [M]. Machinery Industry Press, 2018.

copyright notice
author[von Neumann],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/252/202209090324018189.html

Random recommended