preface
In the previous study, we have been exposed to the output format of str.format(). In this section, we will further learn the relevant contents of string printout, and feel the convenience of using str.format() output through a series of small examples.
practice
Let's first define two variables:
animal = "cow" item = "moon"
If we want to output the sentence "The cow jumped over the moon" by string splicing, we need to use the following code:
print("The "+animal+" jumped over the "+item) >>> The cow jumped over the moon
We mentioned earlier that {} placeholders are required for str.format() format output. As long as the number of {} is the same as the number of variables and the order of variables is consistent with our expectations, we can print the desired results:
print("The {} jumped over the {}".format(animal, item)) >>> The cow jumped over the moon
Two variables are used in this example, so we need two {} placeholders. At the same time, the first position expects to print "cow", so we put the variable animal in the first position and the second position expects to print "moon", so we put the variable item in the second position. If we reverse the order of variables, the printed content will also change:
print("The {} jumped over the {}".format(item, animal)) >>> The moon jumped over the cow
Of course, we can also specify which variable the current placeholder represents through the index. Just fill the position index of the variable into the {} placeholder:
print("The {0} jumped over the {1}".format(animal, item)) >>> The cow jumped over the moon
It is not difficult to find that the location index of variable animal is 0, so {0} replaces animal, and the location index of variable item is 1, so {1} replaces variable item. Then, changing the index in the placeholder will also change the output result:
print("The {1} jumped over the {0}".format(animal, item)) >>> The moon jumped over the cow
Obviously, the two variables exchange positions because the index in the placeholder has changed. By specifying the position index, we can print a variable multiple times:
print("The {1} jumped over the {1}".format(animal, item)) >>> The moon jumped over the moon
Both placeholders point to the variable with position 1, so both positions are replaced with the variable item. So can we directly specify the variable name and assign a value to it when printing like a function? In fact, it is possible:
print("The {animal} jumped over the {item}".format(animal="cow", item="moon")) >>> The cow jumped over the moon
By assigning a value to the variable and specifying the variable name inside the placeholder, we can also fill in the variable content to the location of the placeholder. We can change the final output result by changing the name of the variable represented by the placeholder:
print("The {item} jumped over the {animal}".format(animal="cow", item="moon")) >>> The moon jumped over the cow
It can be seen that after the variable name filled in the first placeholder is changed to {item}, the variable it represents has also changed. Just like using indexes, we can also specify the same variables for different locations:
print("The {animal} jumped over the {animal}".format(animal="cow", item="moon")) >>> The cow jumped over the cow
We can even assign the output format with placeholder to a variable, and then further print out based on this variable:
text = "The {} jumped over the {}" print(text.format(animal, item)) >>> The cow jumped over the moon
From the above, it is not difficult to find that using str.format() format to output strings is more flexible. Next, we introduce the output format based on str.format(). Let's create a new variable name and print the variable:
name = "Tom" print("Hello, my name is {}".format(name)) >>> Hello, my name is Tom
We can specify to reserve a width of 10 characters for variables during string output:
print("Hello, my name is {:10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you
It can be seen that since Tom is only 3 characters wide, a space of 7 characters wide is generated. In addition, we can also specify the alignment of variables:
print("Hello, my name is {:<10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you
The above is left alignment, and we can also right alignment:
print("Hello, my name is {:>10}. Nice to meet you".format(name)) >>> Hello, my name is Tom. Nice to meet you
You can even center:
print("Hello, my name is {:^10}. Nice to meet you".format(name)) >>> Hello, my name is Tom . Nice to meet you
For numerical output, we can specify the number of decimal places to be reserved:
number = 3.14159 print("The number pi is {:.3f}".format(number)) >>> The number pi is 3.142
Here we specify to the nearest 3 decimal places and round. For integers, we can also print them in many ways:
number = 1000 print("The number is {:,}".format(number)) >>> The number is 1,000
We can add a comma every 3 bits, or perform binary conversion, for example:
print("The number is {:b}".format(number)) >>> The number is 1111101000
Convert to octal:
print("The number is {:o}".format(number)) >>> The number is 1750
Convert to hex:
print("The number is {:x}".format(number)) >>> The number is 3e8
And expressed by scientific counting:
print("The number is {:e}".format(number)) >>> The number is 1.000000e+03
The above is all the content of format output. Thank you for your collection, praise and comments. We will introduce Random Numbers in the next section. Please look forward to it~