Author: Jiang Xia

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

| CSDN:blog.csdn.net/qq_4115394…

| the nuggets: juejin. Cn/user / 651387…

| public no. : 1024 notes

This article contains 1215 words and takes 8 minutes to read

In programming, we often need to format the output content and then output it, such as the mobile phone subscription message content we often receive, “Dear *** users, your consumption amount this month is ***”, the content represented by *** here is dynamic output according to the specific content, it is necessary to use the formatted output, So how do you format string output in Python? In fact, similar to other programming languages, need to use placeholders, is to use specific placeholders to replace the content of the above ***, according to the content of the above *** is different, placeholders are also different, commonly used placeholders are the following:

A placeholder Alternative content
   %s      string
    %d         The integer
  %f    Floating point Numbers
%x     A hexadecimal integer

For example, %.3f is a floating point number with two decimal places and a number of placeholders in the string. % is followed by a number of variables or values, and the order of data types must be appropriate. If there is only one placeholder, the parentheses after % can be omitted.

Such as:

%d is an integer placeholder. For 175865523221 inside the parentheses, the content after the # replacement should be parentheses. And the content between the string and the replacement should be added %. Otherwise, everything inside the braces is printed as a string # and it's ok to use string placeholders for anything, # because the previous content is a string # In Python, the comment is #, # as opposed to // or /**print(%s, hello, Print (%s, hello, print(%s %,175865523221)Copy the code

The output

# float placeholder,.3.2 represents the number of decimal places after the decimal point, only one placeholder, Print (%.3f % 2.568976) print(%.3f % 2.568976) print(%.3f % 2.568976) print(%.3f % 2.568976)Copy the code

The results are as follows

Print (%.3d %3) print(% 03D %3) print(%3d %3) print(%d %3)Copy the code

Result output: Note the difference in the markup

A hexadecimal integer

Print (%x % 3) print(%x % 322)Copy the code

The output

Another way to format a string is to use the string format() method, which replaces {0},{1}…… within the string with the parameters passed in For example:

Print ({0})print({0})print({0})print({0})print({1}. Print ({0}, hello, please call {1:d}. Format (liu,175865523221)) print({1}.Copy the code

Output:

So that’s a simple use of string formatting in Python!

Finally, welcome to pay attention to the public number: 1024 notes, free access to massive learning resources (including video, source code, documents)!

Other recommendations:

  • Spring annotation (3) : @scope sets the scope of the component
  • Spring is worth your collection!!
  • Spring annotation (7) : Assign attributes to beans using @value
  • Spring Note (5) : Four ways a container can register a component
  • Spring Note (6) : Four ways to customize initialization and destruction methods during the Bean’s life cycle