in python print usage

print usage

Reference documents:
https://blog.csdn.net/sinat_28576553/article/details/81154912

 

table of Contents

A, print () function Overview

Second, the output variables

Third, the output data formatted

3.1% character

3.2 minimum field width and precision

3.3 conversion flag

3.4 format characters induction

Fourth, wrap and wrap to prevent

A, print () function Overview
print () method for printing out, the most common is a python function.

The syntax of this function is as follows:

print (* objects, sep = ' ', end = '\ n', file = sys.stdout)
parameter is explained as follows:

objects - objects that represent the output. A plurality of output objects, need, (comma).

sep - for a plurality of spaced objects.

end - the end of what used to be set. The default is a newline character \ n, we can be replaced with other characters.

file - the file object to write.

print (1) # numeric type can be directly output

'' '
results are as follows
. 1
' ''

Print ( "the Hello World") can be directly output string type #

'' '
results are as follows:
the Hello World
' ''

A. 1 =
B = "the Hello World"
Print (A, B) # may output a plurality of objects, objects separated by commas
'' '
results are as follows:
. 1 the Hello World
' ''

# if the direct output string, instead of the object represented by you can not use a comma
Print ( "Duan" "Yixuan")
Print ( "Duan", "Yixuan")

'' '
results are as follows:
DuanYixuan
Duan Yixuan
understood, comma separator was not added, there is no gap between the strings
' ''

Print ( "WWW", "SNH48", "COM", On Sep = ".") # Set character spacing
'' '
results are as follows:
www.snh48.com
' ''
Second, the output of the variable
no matter what type of data, including but not limited to: numeric, boolean, list of variables, a variable dictionary .. . can be directly output.


# For example:

NUM =. 19
Print (NUM). 19 # numeric variable output

STR = 'Duan Yixuan'
Print (STR) #Duan Yixuan output string variable

List = [1,2, 'A']
Print (List) # [ 1, 2, 'a'] output list variable

tuple = (1,2, 'A')
Print (tuple) # (. 1, 2, 'A') output tuple variable

dict = { 'a': 1 , ' B ': 2}
Print (dict) {#' a ':. 1,' B ': 2} output dictionary variable
three, formatted output data
in the C language, we can use printf ( "% - 4f." , in the form of a) or the like, data of the formatted output.

In python, we can achieve the same format the output data. We can look at a simple example:

S = 'Duan Yixuan'
X = len (S)
Print ( 'of The length of% S IS% D'% (S, X))

'' '
' of The length of% S IS% D 'This part is called: Format Control character
(s, x) this part is called: conversion specifier
% character, indicates the start tag conversion specifier
following output:
of the length of Duan Yixuan iS. 11
'' '
the difference between the C language is that, Python format control characters and conversion with separated specifier%, C language with a comma.

Then we carefully look into formatted output

Character 3.1%
(1)% characters: Conversion described Start tag breaks.

Usage% characters refer to the embodiment, it is omitted.

3.2 minimum field width accuracy and
the minimum field width: the converted string should have at least the value of a specified width. If * (asterisk), the width will be read from the value tuple.

Followed by precision value point (.): If the real output, the precision of decimal places is now shown after. If desired output string, then the number means the maximum field width. * If it is, then the accuracy will tuple read out.

Refer to implementation of the C language.

Note: The field width, also accounted for a decimal point.

= 3.141592653 the PI
Print ( '% 10.3f' the PI%) 10 # field width, precision 3
# 3.142

# 3 precision, so only 142, the specified width is 10, so the need to add five spaces left to reach 10 the width of
the PI = 3.1415926
Print ( "the PI =%. * F"% (. 3, the PI))
# * read a field width or precision from behind tuple, can be read out accuracy is three
# PI = 3.142

# width is not specified, it is not necessary to indent

print ( "PI =% *. 3f"% (10, PI)) # 3 accuracy, a total length of 10. the
# = 3.142 the PI

different positions in which the # * read the content is also different from
3.3 Switch flag
Switch flag: - for left-aligned; + means to add value before the sign; "" (blank character) expressed reservations spaces (before a positive number); 0 indicates that the converted value if not enough digits it is filled with zeros.

We can look at a specific example:

= 3.1415926 the PI
Print ( '% - 10.3f' the PI%) # Left, or 10 characters, but the space on the right.
3.142 #
the PI = 3.1415926
Print ( '% + f' the PI%) + # 3.141593 # sign display
default precision f of type # 6 decimal places.
= 3.1415926 the PI
Print ( '% 010.3f' the PI%) # field width 10, a precision of 3, the blank is filled with zeros at less than
# 000003.142 conversion value 0 indicates the number of bits is not enough if it is filled with zeros
3.4 inductive character format
Format Character Description format character Description

% S using string str () to display a hexadecimal integer% x

% R string display (the repr ())% e index of the (substrate write e)

% E% c single character index (substrate write E)

% B binary integer% f,% F float

% G% d decimal integer exponent (e) or floating-point (the length of the display)

% I% G decimal integer exponent (E) or float (the length of the display)

% O octal integer character %%%

Fourth, wrap and prevent wrapping
in python, the output function is always the default line break, for example:

the X-in the Range for (0,5):
Print (the X-)

'' '
0
1
2
3
4
' ''
and obviously, this output takes up too much "space", we can transform the following:

Reference text description of the first portion end parameters: end - the end to what to set. The default is a newline character \ n, we can be replaced with other characters.

Range in X for (0,. 5):
Print (X, End = '')
# 0. 4. 3. 1 2
for X in Range (0,. 5):
Print (X, End = ',')
# 0,1, 2,3,4,
but if we run above two codes, the results will be shown below, it is understood: we need between the two outputs to achieve wrap.

Range in X for (0,. 5):
Print (X, End = '')
for X in Range (0,. 5):
Print (X, End = ',')

# 0. 4. 3. 1 0,1,2 2 , 3 and 4,
we compare several ways

method one:

for X in Range (0,. 5):
Print (X, End = '')

Print ( '\ n-')

for X in Range (0,. 5):
Print (X, End = ',')

'' '
0 1234
0,1,2,3,4,
''
this happens above, because the print () itself is the default line feed, plus the newline character, line feed equivalent to two.

Second way:

for X in Range (0,. 5):
Print (X, End = '')

Print () # comes with its own wrap, perfect output

for X in Range (0,. 5):
Print (X, End = ',')

'' '
01234
0,1,2,3,4,
' ''

Guess you like

Origin www.cnblogs.com/yxqnote/p/11641555.html