Format output
python formats output in two ways: a percent sign and a format
The function of format is more powerful than the percent sign method. The unique functions of format, such as custom character filling in blank, string centered display, binary conversion, automatic integer segmentation, percentage display and so on, cannot be compared with the percent sign method
1. % formatted output
Is a placeholder that will be replaced by the value of an expression (number, string, addition, subtraction, multiplication and division, variable, constant, etc.)
1.1 output in sequence by position
print('full name%s, Age%d year' % ('Zhang San', 20)) # >>>Name: Zhang San, age: 20
1.2 custom key output
print("full name%(name)s, Age%(age)d year" %{'name':'Zhang San', 'age':20}) # >>>Name: Zhang San, age: 20
1.3 custom width, decimal, alignment
print("full name%(name)+2s, Age %(age)d year, height %(height).2f rice" % {'name':'Zhang San','age':20,'height':1.7512}) # >>>Name: Zhang San, age: 20, height: 1.75m #Define a name with a width of 2 and align it right Define height as floating-point type, with 2 decimal places reserved
1.4 binary conversion
print( "Original number: %d, octal number system:%o , hexadecimal:%x" % (15,15,15)) # >>>Original number: 15, octal: 17, hexadecimal: f
1.5 percentage display
print("Percentage display:%.2f%%" % 75) # >>>Percentage display: 75.00%
1.6% format output character
- %d. % i: convert integers and floating-point numbers to signed decimal representation
- %r: Use the repr() function to convert the expression to a string
- %s: Use the str() function to convert the expression to a string
- %o: Converts an integer to a signed octal representation
- %x. % X: convert integer to signed hexadecimal representation
- %E% e: convert integers and floating-point numbers to floating-point numbers represented by scientific counting method
- %f. % F: convert integer and floating point number to decimal floating point number
- %g: Intelligent selection uses% f or% e format to automatically adjust the integer and floating-point number into floating-point or scientific counting method (scientific counting method is used for more than 6 digits), and formats it to the specified position (E in case of scientific counting)
- %G: Smart selection uses% F or% e format, automatically adjusts, converts integers and floating-point numbers to floating-point or scientific counting (scientific counting is used for more than 6 digits), and formats them to the specified position (E in case of scientific counting)
- %c: Format character and its ASCII code, integer: convert the number into the value corresponding to its unicode, and the decimal range is 0 < = I < = 1114111 (py27 only supports 0-255); Character: adds a character to a specified position
- %+Right alignment; Plus sign before positive number and minus sign before negative number;
- %-Left alignment; No sign before positive number, plus minus sign before negative number;
- %Space right justified; Positive numbers are preceded by spaces and negative numbers by minus signs;
- %0 right alignment; No sign before positive number, plus minus sign before negative number; Fill the blanks with 0
- %, when there is a format flag in the string, you need to use%% to represent a percent sign. Note: in Python, the percent sign format does not automatically convert integers to binary representation
2. Format format
The format function takes the string as a template and formats it through the passed in parameters, which are represented by curly braces {}; Format conversion uses': 'after placeholder, expression {X: F} format(x=99.9)
There are known variables:
name = 'Zhang San' age = 20 sex = 'male' score = 99.9
1. Through position matching
1.1 if the location is not specified, the format value will be passed into the default location in turn.
print('name{}, Age{}year, Gender{}'.format(name, age, sex)) # >>>Name: Zhang San, age: 20, gender: Male
1.2 specify the location, and the format is passed in according to the specified subscript.
print('name{1}, Age{1}year, Gender{1})'.format(name, age, sex)) # >>>Name 20, age 20, gender 20
2. Match by name
2.1 index matching parameters
print('full name{name}, Age{age}Age, gender{sex}, fraction{score:.2f}' .format (name = 'Zhang San', age = 18, sex = 'male', score = 99.9)) # >>>Name: Zhang San, age: 18, gender: male, score: 99.9
2.2 dictionary matching parameters
info = {'name': 'Zhang San', 'score': 99.9, 'age': 18, 'sex': 'male', 'percentage': 0.99} print('full name{name}, Age{age}Age, gender{sex}, fraction{score :4.3f} , percentage{percentage:.3%}'.format(**info)) # >>>Name: Zhang San, age: 18 years old, gender: male, score: 99.900, percentage: 99.000%
2.3 format deformation usage, expressed in the form of f '{}'
print(f'full name{name}, Age{age}Age, gender{sex}, fraction{score:4.3f}') # >>>Name: Zhang San, age: 20, gender: male, score: 99.900
Extension: common escape characters
- (at the end of the line) continuation character
- \Backslash symbol
- \'single quote
- \"Double quotation mark
- \a ring
- \b backspace
- \e escape
- \000 empty
- \n line feed
- \v vertical tabulation
- \t horizontal tabulation
- \r enter
- \f page turning