Python print () function Advanced Usage

Previously used print () function, the output of only one variable, but actually print () function can output a plurality of variables simultaneously, and it has a more feature-rich.

print () function has the following syntax in detail:

print (value,...,sep='',end='\n',file=sys.stdout,flush=False)

As can be seen from the above syntax, value parameter can take any number of variables or values, so print () function can output a plurality of values. For example the following code: Marble member repair factory
  1. user_name = 'Charlie'
  2. user_age = 8
  3. # Simultaneously output a plurality of variable strings and
  4. print ( "Readers name:", user_name, "Age:", user_age)
Running the above code, the following output can be seen:

Readers Name: Charlie Age: 8

From the output perspective, to print () function when a plurality of output variables, print () function multiple variables default separated by a space, if the reader wishes to change the default delimiter, the parameters can be set by sep. For example output statement:
  1. # Simultaneously output a plurality of variables and strings, specify the delimiter
  2. Print ( "Readers name:" , user_name , "Age:" , user_age , sep = '|' )
Running the above code, the following output can be seen:

Readers name: | Charlie | Age: | 8


By default, print () function after the output is always wrap, because print () function parameters default value end is "\ n", the "\ n" represents the newline. If desired print () function does not change after the output line is reset to end the parameter, for example the following code:
# Set end parameter, after a specified output is no longer wrap 
Print (40, '\ T', end = "") 
Print (5O, '\ T', end = "") 
Print (60, '\ T', end = "")
The above three print () statement will be executed three times the output, but since they have designated end = "", therefore each print () output statements will not wrap, still on the same line. Running the above code, the following output can be seen:

40    50    60


parameter specifies file print () function of the output destination, the default value sys.stdout file parameter, which represents the default value of standard output system, i.e. the screen, so print () function is output to the screen by default. Indeed, you can allow the parameter by changing the print () function to the output of a particular file, for example the following code:
  1. F = Open ( "demo.txt" , "W" ) # open the file for writing
  2. Print ( 'sea pearl tears' , File = F )
  3. Print ( 'blue smoke Press Warm Jade' , File = F )
  4. f.close()
In the above procedure, open () function opens the file demo.txt, successive two segments 2 that will print function string sequentially written this file, the last call close () function closes the file, will be described in detail later sections tutorial content on file operations.

flush parameter print () function for controlling an output buffer, which is generally held to False parameter can, so that better performance can be obtained.
 

Guess you like

Origin www.cnblogs.com/furuihua/p/12560087.html