This article is participating in Python Theme Month. See the link for details

My friends, for reprint please indicate the source: blog.csdn.net/jiangjunsho…

Disclaimer: During the teaching of artificial intelligence technology, many students asked me some python related questions, so in order to let students master more extended knowledge and better understand AI technology, I asked my assistant to share this Python series of tutorials, hoping to help you! Since this Python tutorial is not written by me, it is not as funny and boring as my AI teaching. But its knowledge points or say in place, also worth reading! PS: if you don’t understand this article, please read the previous article first. Step by step, you won’t feel difficult to learn a little every day!

Python also provides a more advanced way to combine string processing tasks — string formatting allows multiple specific types of substitutions to be performed on a string in a single step. It’s handy, especially when formatting text for display to the user. Because the Python world is full of new ideas, string formatting in Python can now be implemented in two forms.

One form is string formatted expressions, an original technique that dates back to Python. This is the “printf” model based on C language.

Another form is string formatting method calls, which are new to Python 2.6 and Python 3.0. These are unique to Python and have a lot of overlap with string formatting expressions.

Let’s look at the first form, string formatted expressions.

Python defines the % binary operator for strings (which, when applied to numbers, is the remainder operator for division). When applied to strings, % provides an easy way to format string values. In short, the % operator provides a neat way to write multiple string substitutions instead of building and combining individual parts.

1. Place a formatted string with one or more embedded conversion targets, all beginning with % (for example, %d), to the left of the % operator. 2. Place an object (or objects) to the right of the % operator, which will be inserted into the position of one (or more) conversion targets on the left where you want Python to format the string.

For example, in the example below, the integer 1 replaces %d to the left of the formatted string, and the string ‘dead’ replaces %s. The result is a new string, which is the result of these two substitutions:

>>> 'That is %d %s bird!' % (1,'dead')  # Format expression

That is 1 dead bird!
Copy the code

Formatting allows us to combine multiple steps into a single operation, which is quite powerful. Let’s look at a few more examples:

>>> exclamation = "Ni" >>> "The knights who say %s!" % exclamation 'The knights who say Ni! '> > > "% d % d % s you" % (1, "spam", 4)' 1 spam 4 you '> > > "% s, % s, % s" % (42,3.14159, [1, 2, 3])' 42-3.14159 - [1, 2, 3] 'Copy the code

In the first example, insert the string ‘Ni’ at the left target position in place of the token %s. In the second example, three values are inserted into the target string. Note that when more than one value is to be inserted, you should enclose them in parentheses on the right (that is, place them in a tuple). The third example also inserts three values: an integer, a floating-point object, and a list object. But notice that all targets are %s to the left, which means converting them to strings. Because each type of an object can be converted to a string, each object type that participates in the operation with %s can be converted to code. Because of this, unless you want to do special formatting, you generally only need to remember to format expressions with %s code.

Also, remember that formatting always returns a new string as a result rather than modifying the string on the left; Since strings are immutable, this is the only way to operate.