Problems of print Formatted Output in python

  • 2021-10-27 07:55:29
  • OfStack

Preface

Good afternoon, Fubao. The reading volume of Chapter 4.5 after the revision is a little less. I don't know if it was delayed for one day. Woo-woo, the next 1 must be a good bear without dragging, so continue to explode the liver today Chapter 5: print formatted output. I used one of them when I wrote Chapter 3: the combination of "f … {}". There are some doubts in the previous comments, so let's explain this thing in detail today, and there are other formatting methods. Then let's start today's study.

Tip: The following is the main content of this article, and the following cases can be used for reference

1. f-String formatting

The so-called formatted output is to create a string that can embed the contents of variables. If it is not formatted, the variable name will be printed directly, which will not play its role. So let's start with what we used earlier: f-String formatting. The format, as mentioned above, is f "… {} …", where you want to call a variable in a string, you need to write the variable in "{}" and write "f" at the beginning of the string. f stands for format, which means formatting. This combination is fixed, and one is indispensable. If you write "f", it is equivalent to telling python, which is a formatted string. python will look at those variables. Of course, if you write f in front and don't write brackets in the middle, you can also execute normally. I just didn't call the variable.


name = " Moon Bear "
name2 = " Han Han Bear "
age = 18
age2 = 19
print(f"{name}: Good afternoon, everyone. I'm {name},{age} Young literary and artistic people. This is my iron {name2}, He {age2} Please take more care of me ")
print(f"{name2} : Whoa, whoa, whoa ~")

RUN:

Chi Yue Xiong: Good afternoon, everyone. I am Chi Yue Xiong, an 18-year-old literary youth. This is my iron son Han Han bear, he is 19, please take care of him
Han Han Bear: Ow Ow ~

2. Placeholders

First of all, look at a table, common placeholders

符号 意义
%d 整数
%f 浮点数
%s 字符串

Placeholder, as the name implies. Is to take one position in one string, and then insert a reference variable. The specific format is print ("…% d …"% (variable)). Let's look at the effect directly through exercises.


name = " Han Han Bear "
once = 1
num = 4.2
print("%s  His favorite thing is playing games, and he is best at action adventure " % name)
print(" Contra, he even needs  %d  You can clear customs with your life " % once)
print(" Even though both his eyes have been %f It's over, but I still can't put down the game machine " %num)

RUN:

Han Han Bear's favorite thing is to play games, and his best is action adventure
Contra, he even needs only one life to clear customs
Even though his eyes are 4.200 million degrees, he still can't put down the game machine

The effect is like this, of course, you can also put it in one try.


name = " Han Han Bear "
once = 1
num = 4.2
print("""
%s  His favorite thing is playing games, and he is best at action adventure .
 Contra, he even needs  %d  You can clear customs with your life .
 Even though both his eyes have been %f It's over, but I still can't put down the game machine 
""" % (name,once,num))

RUN:

Han Han Bear's favorite thing is playing games, and he is best at action adventure.
Contra, he even needs only one life to clear customs.
Even though his eyes are 4.200 million degrees, he still can't put down the game machine

When using placeholders, 1 must think about the data type of the variable. If you write a% d, you will report an error if you put a string in it. Of course, numbers can also be used as strings, and you can write% s as well. However, for the convenience of browsing, it is best to fill in the corresponding placeholders according to the data type. Also, if your placeholder is% d, and you put a floating point type in it, it will cast you to an integer, and vice versa. As follows:


num1 = 4.2
num2 = 4
print("%d" % num1)
print("%f" % num2)

RUN:

4
4.000000

3. format Formatting

The third is to use format function, which is also called method in python. The basic syntax is to write "{}" to the string, and finally call the. format method at the end of the string to insert the variable into {}. Does it look like f-string formatting, format formatting is updated in python 2.6, and f-string is updated in python 3.6. Of course, I personally think this is uncomfortable to use, but the above two are better to use. Many times, if you want to insert variables, you have to write a lot of parameters after them. What trouble! Here's the effect.


name1 = " Moon Bear "
name2 = " Han Han Bear "
time = 12
num = 2
print(" At this time in the middle of the night {} Point: \" Whoo-hoo-hoo-hoo , {}.\"".format(time,name1))
print("{name2}, Big night {time} Point , What are you screaming about ".format(time=time, name2=name2))
print(" I can't believe I spent it {} It took my life to pass this level ".format(num))
print(f"{name1} Monologue in my heart: This thing can 1 It's not a normal bear whose life has passed. ".format(name1=name1))
print("< Made in Mario >....")

RUN:

At 12 o'clock in the middle of the night: "Woo Woo, crazy moon bear."
Han Han Bear, it's 12 o'clock in the evening. What are you screaming at
I can't believe it took me two lives to get through this level
Monologue in the heart of Chi Yue Bear: It's not a normal bear that this thing can pass with one life.
< Made in Mario > ....

Summarize

Personally, I like to use f-string formatting, but I still have to show you about it. Today's chapter is written in one block with 4.5, one collection of data and exercises. To sum up, I can copy the summary of the previous chapter. OK, today's summary is Ollie! Another passionate day, slipping away ~


Related articles: