Detailed explanation of format() method in Python

Y2J
Release: 2017-04-21 14:55:44
Original
1802 people have browsed it

This article mainly introduces relevant information about the format() method in Python. Friends who need it can refer to

Detailed explanation of the format() method in Python

In Python, the formatted output string uses the format() function. Strings are classes, and you can use methods;

Python is a completely object-oriented language. Everything is an object;

The parameters of the string are represented by {NUM}, 0, represents the first parameter, 1, represents the second parameter, will be used in the future times increment;

Use ":" to specify the operations required to represent the element, such as ":.3" with three decimal places, ":8" taking up 8 character space, etc.;

You can also add specific letters, such as:

  1. 'b' - binary. Output the number in base 2.

  2. 'c' - Character. Convert the integer to the corresponding Unicode string before printing.

  3. 'd' - Decimal integer. Convert the number to base 10 Output.

  4. 'o' - Octal. Output numbers in base 8.

  5. 'x' - Hexadecimal . Output the number in base 16, and use lowercase letters for digits above 9.

  6. 'e' - Power symbol. Print the number in scientific notation, represented by 'e' Power.

  7. 'g' - General format. Output the value in fixed-point format. When the value is particularly large, print it in power form.

  8. 'n' - Number. Same as 'd' when the value is an integer, and the same as 'g' when the value is a floating point number. The difference is that it inserts a number separator according to the locale.

  9. '%' - Percentage. Multiply the value by 100 and print it in fixed-point ('f') format, with a percent sign after 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)) #输出参数 
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)) #使用_补齐空位 
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')) #指定宽度
Copy after login

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  .
Copy after login

The above is the detailed content of Detailed explanation of format() method in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template