preamble
Good afternoon, Fubo. I'm not sure if it's because I've been dragging my feet all day. I'm not sure if it's because I've been procrastinating all day. I'm going to be a good bear who doesn't procrastinate, so I'm going to continue to work on Chapter 5: Formatting Output with PRINT. I used one of these when I was writing Chapter 3: the " f ... {} " combination. Before the comments below a partner puzzled, then today we will explain this thing in detail, in addition to other ways of formatting. So let's start today's study.
Tip: Below is the body of this post, with the following examples for reference
I. f-String Formatting
By formatted output, I mean creating a string that can be embedded with the contents of a variable. Without formatting, the name of the variable would be printed out directly and would not serve its purpose. So let's start with what we used before: f-String formatting. As mentioned above, the format is: f" ... {} ... ", where you want to call the variable in a string, you need to write the variable in " { } ", and you need to write " f " at the beginning of the string. f stands for format, which means formatting. The combination is fixed, and you can't have one without the other. When you write " f ", you're telling python that it's a formatted string, and python is going to look at the variables, but of course, if you write the f in front of the string and don't write the parentheses in the middle of the string, it's going to work just fine. It just doesn't bother calling the variables.
name = "Moonstruck Bear." name2 = "Bean Bear." age = 18 age2 = 19 print(f"{name}:Good afternoon, gentlemen.,I am.{name},{age}artistic youth。This is my Tieh Tzu.{name2},(used for either sex when the sex is unknown or unimportant){age2},please treat me kindly (conventional greeting on first meeting)") print(f"{name2}:Ouch.~")
RUN:
I'm a young man of 18. This is my son, Bean Bear. This is my son Bean Bear, he is 19, please take care of him.
Bean Bear: Owwwww!
II. Placeholders
First of all, it's better to look at a table of common placeholder symbols
notation | significance |
---|---|
%d | integer (math.) |
%f | floating point |
%s | string (computer science) |
A placeholder, as the name implies. It is a placeholder in a string, and then refer to the variable inserted. The specific format is print("...%d..." % (variable)), the following is still directly through the exercise to see the effect.
name = "Bean Bear." once = 1 num = 4.2 print("%s favorite thing to do is play games, and he's still best at the action-adventure genre" % name) print("Contra he even only needs %d lives to pass." % once) print("Even though he has %f degrees in both eyes, he still can't put the console down." %num)
RUN:
Bean Bear's favorite thing to do is play games, and he's best at the action-adventure genre.
Contra, he only needed one life to get through.
Even though both of his eyes are 4.200000 degrees, he still can't put down the console
This is how it works, but of course you can try it together.
name = "Bean Bear." once = 1 num = 4.2 print(""" %s favorite thing to do is play games, and he's still best at the action-adventure genre. He can even get through Contra with only %d lives. He can't put down the console even though both of his eyes are already %f degree. """ % (name,once,num))
RUN:
Bean Bear's favorite thing to do is play games, and he's best at the action-adventure genre.
Contra he even needs only 1 life to pass.
Even though both of his eyes are 4.200000 degrees, he still can't put down the console
When using placeholders, make sure you think about the data type of the variable, if you write a %d and you end up putting a string in it will report an error. Of course, numbers can also be used as strings, and you can write them as %s. But to make it easier to navigate, it's best to fill in the placeholders according to the data type. In addition, if you placeholder is %d, you put a floating-point type into it, he will force you to convert to an integer, and vice versa. As follows:
num1 = 4.2 num2 = 4 print("%d" % num1) print("%f" % num2)
RUN:
4
4.000000
III. Formatting
The third one is to utilize the format function, also called method in python. The basic syntax is to write " {}" in the string and finally call the .format method at the end of the string to insert the variable into {}. Doesn't it look like f-string formatting, format formatting was updated in python 2.6 and f-string in python 3.6. Of course personally I don't think this is very comfortable to use, it's better to use the above two. There are many times when you want to insert a variable, you have to write a bunch of parameters after it. It's a pain in the ass! Here's what it looks like.
name1 = "Moonstruck Bear." name2 = "Bean Bear." time = 12 num = 2 print("In the middle of the night.{}point (in space or time):\"Woo-hoo!, {}.\"".format(time,name1)) print("{name2},at night{time}point (in space or time)的,What are you screaming about?".format(time=time, name2=name2)) print("I can't believe I spent{}I had to kill myself to get through this level.".format(num)) print(f"{name1}soliloquy:It's not a normal bear that can get through this thing with one life, is it?。".format(name1=name1)) print("<Made in Mario>....")
RUN:
In the middle of the night.12point (in space or time):"Woo-hoo!, bear with a demented appetite."
Bean Bear, it's 12:00 at night. What are you yelling for?
I can't believe it took me 2 lives to get through this level.
Demented moon bear heart monologue: this thing can a life on the past is not normal bear it.
<Made in Mario>....
summarize
Personally, I love formatting with f-strings, but I'll have to walk you through it. Today's chapter is and 4.5 is a piece of writing, together with the collection of information and exercises to come. The summary of the words, I can copy the last chapter summary over it, okay, then today's summary is Ollie give! It's been another passionate day, so I'm off!
to this article on python print formatted output article is introduced to this, more related python print formatted output content please search my previous posts or continue to browse the following related articles I hope you will support me more in the future!