This article mainly introduces detailed information about the format () method in Python. For more information about the format () method in Python, see the following article, for more information, see
How to format () in Python
The format () function is used to format the output string in Python. a string is a class and can be used;
Python is a fully object-oriented language, and everything is an object;
String parameters are represented by {NUM}, 0, indicating the first parameter, 1, indicating the second parameter, followed by increments;
Use ":" to specify the operations required by the element, for example, ":. 3" three decimal places, ": 8" occupies 8 character space;
You can also add specific letters, such:
-
'B'-binary. outputs the number based on 2.
-
'C'-character. convert the integer to the corresponding Unicode string before printing.
-
'D'-a decimal integer. The number is output based on 10.
-
'O'-octal. The number is output based on 8.
-
'X'-Hexadecimal. The number is output based on 16, and the digits over 9 are lowercase letters.
-
'E'-power symbol. print the number by scientific notation and use 'e' to represent the power.
-
'G'-general format. The value is output in the fixed-point format. when the value is very large, it is printed in the Power format.
-
'N'-number. when the value is an integer, it is the same as 'D', and the value is the same as that of 'g'. The difference is that it inserts a digital separator according to the region settings.
-
'%'-Percent. multiply the value by 100 and print it in the fixed-point ('F') format. a percent sign will be placed next to the value.
Numbers (0, 1,...) represent the elements in format (), so you can use "." to call the element method;
The code is as follows:
#-*-Coding: UTF-8-*-# = # File: abop. py # Author: Wendy # Date: 2013-12-03 #==============================#eclipse pydev, python3.3 age = 25 name = 'Caroline 'print (' {0} is {1} years old. '. format (name, age) # output parameter print ('{0} is a girl. '. format (name) print ('{0 :. 3} is a decimal. '. format (1/3) # print ('{0: _ ^ 11} is a 11 length. '. format (name) # Use _ to fill the blank print ('{first} is as {second }. '. format (first = name, second = 'Wendy ') # print ('My name is 0.name+'.format(open('out.txt', 'w '))) # Call method print ('My name is {0: 8 }. '. format ('Fred ') # specify the width
Output:
Caroline is 25 years old. Caroline is a girl. 0.333 is a decimal. _Caroline is a 11 length. Caroline is as Wendy. My name is out.txt My name is Fred .
The above is the detailed description of the format () method in Python. For more information, see other related articles in the first PHP community!