The format output string in Python uses the format () function, the string is the class, you can use the method;
Python is a fully object-oriented language, and everything is an object;
The parameters of the string are represented by {NUM}, 0, representing the first parameter, 1, representing the second parameter, and then sequentially sliding scale;
Use ":" To specify the actions required to represent the element, such as ":. 3", three decimal Places, ": 8" for 8 character spaces, etc.;
You can also add specific letters, such as:
' B '-binary. The number is output as a base of 2.
' C '-character. Converts an integer to the corresponding Unicode string before printing.
' d '-decimal integer. The number is output as a base of 10.
' O '-eight binary. The number is output as a base of 8.
' x '-16 binary. The number is output in 16, and the number of digits above 9 is in lowercase letters.
The ' e '-power symbol. Use scientific notation to print numbers, and ' e ' for power.
' G '-general format. The value is output in fixed-point format. When the number is particularly large, it is printed in a power form.
' N '-number. When the value is an integer and ' d ', the values are the same as the ' G ' when they are floating-point numbers. The difference is that it inserts a numeric delimiter according to the locale.
'% '-hundred fractions. Multiply the value by 100 and then print in fixed-point (' F ') format, followed by a percent semicolon.
Number (0, 1, ...) Represents the elements inside the format (), so you can use the "." Method of invoking the element;
#-*-Coding:utf-8-*- #==================== #File: abop.py#author:wendy#date: #==================== # Eclipse Pydev, python3.3 age = 25name = ' Caroline ' print (' {0} was {1} years old. '. Format (name, age) #输出参数print (' {0} is a girl. '. Format (name)) print (' {0:.3} is a decimal. '. Format (1/3)) #小数点后三位print (' {0:_^11} is a one-length. '. Format (name) #使用_补齐空位print (' {first} is as {second}. '. Format (first=name, second= ' Wendy ')) #别名替换print (' My name is {0.name} '. Format (open (' OUT.txt ', ' W ')) #调用方法print (' My Name is {0:8}. '. Format (' Fred ')) #指定宽度
Output:
Caroline is years old. Caroline is a girl. 0.333 is a decimal. _caroline is a one-length. Caroline is as Wendy. My name is out.txtmy name is Fred.