Discussion on Format Output of Python

  • 2021-12-09 09:29:29
  • OfStack

Output of python

The print () function in the python output can be directly referenced, and its complete format is:


# There are also file operations at the end, we mainly study output related, so we will not join for the time being 
print(object, sep=' ', end='\n')

object It is something we want to output. It can be a variable or specific data. It can be single or multiple. If it is multiple, it can be separated by ','

sep --When we want to output more than one thing will be used, it indicates that we output more than one data output with what to separate, if the output is not set, it defaults to a space to separate

end -Used to set what to end with after outputting the data we want to output. It defaults to end = '\ n' when you don't set it specifically, so the default print () function wraps lines when the output is finished


print(1,2,3,end='')
print(1,2,3,sep='!')
print(1,2,3,)
# Their output is as follows: 
1 2 31!2!3
1 2 3
# Please think carefully about why this is so and sort out the clue 

We in C language middle school scanf () function is formatted output, can set to output the data format and data form, that we in python how to achieve?


int a=10;
float b=6.6;
scanf("%d%f\n",a,b);

The above C language code is implemented in python as:


a=10
b=6.6
print("%d%f"%(a,b))
#print(a,b,sep='') 

If there is a request for the output data format, we can choose the above way, and c language 1 for the output format modification settings

If the output format does not require us to choose the second method, it is more convenient. When outputting with string 1, we can use + to connect, but the premise is that the output type is also a string type. If it is not a string type, we can use str () function to cast and connect the output, as follows:


a=18
print("Her age is "+a+" !")

It is wrong to write like this, because a is assigned to 18, so the data type of a is integer, and integer cannot be directly connected with string by +, so the correct writing should be:


a=18
print("Her age is "+str(a)+" !")
# The output is: Her age is 18 !

Next, I will specifically talk about the formatted output data under 1. The formatted output of% is similar to that of C language:

% d--Decimal integer

% o--8-ary integer

% x--106-ary integer

% f-Floating point number, 6 significant digits after the decimal point

% e-Exponential output, 6 significant digits after decimal point

% g-6 significant digits, decimal, otherwise, scientific notation


a=5
print(10)
print("%d"%a)
print("%2d"%a)# When outputting, follow 2 Output of space size, right alignment by default 
print("%-2d"%a)#2 Space size, because d The front is - Number, so align to the left 
print("%02d"%a)#2 Output of space size, which is not enough 0 Complement 
print("%-02d"%a)#2 Output of space size, which is not enough 0 Complement, left alignment 

# The output from the above code is:
10
5
5
5
05
5

Summarize the above code under 1. Between% and d, you can add 1 number or symbol to control the format of output data, specifically as follows:

% (control left-right alignment) (complete with what) (output according to several bits of space) d are:

The default is right alignment, and the minus sign is left alignment

Default blank filling, and other filling can be set

When the number itself exceeds the required space, there is no impact. When the data itself is insufficient, it is aligned according to left and right, and filled according to. To put it bluntly, it is to occupy a position

Other data types are also similar, whether floating point or string, such as% 5.2 f represents, occupy 5 positions, and retain two decimal places, to pay special attention to the floating point decimal point is also occupied 1 position, next look at a few examples, a good experience under 1


b=5.21
print(b)
print("%f"%b)
print("%2.1f"%b)
print("%2.2f"%b)
print("%5.2f"%b)
print("%5.5f"%b)
print("%.5f"%b)

# The output above is:
5.21
5.210000
5.2
5.21
5.21
5.21000
5.21000
# Everyone pay attention to the comparative study

Some things must be realized by themselves to write down. I hope everyone can think diligently and don't be lazy!

Next, introduce a function- str.format()

format () is more powerful than the '%' approach to basic formatted output, which treats the string as a template, formats it with the parameters passed in, and replaces'% 'with curly braces' {} 'as special characters

Don't talk too much about the code! ! !


# Through position matching, similar to arrays 
print('{} {}'.format('hello','baibai'))#hello baibai
print('{1} {0}'.format('hello','baibai'))#baibai hello
print('{1} {1}'.format('hello','baibai'))#baibai baibai
print('{0} {0}{0}'.format('hello','baibai'))#hello hellohello
# Matching by corresponding value names, similar to dictionaries 
# (You can also pass in here 1 Dictionary type key And value Corresponding to) 
print('{a},{b}'.format(a='1',b='8'))#1,8

In addition to these, we can also output more complex data types


# Or use 1 What I defined before 1 Data 
goods=[(1," Pepsi Cola ",10,3),(2," Master Kong instant noodles ",15,2),(3," Mug ",5,30)]
list=[1,2,3]
print('{0[1]}'.format(goods))#(2, ' Master Kong instant noodles ', 15, 2)
print('{0[1]},{1[2]}'.format(goods,list))#(2, ' Master Kong instant noodles ', 15, 2),3
print('{0[1][1]}'.format(goods,list))# Master Kong instant noodles 

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: