Format () is used to format the output string in Python.

Source: Internet
Author: User
The format () function is used to format the output string in Python. a string is a class and can be used as a method. 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 & quot;: & quot ;, specifies the operations required by the element, such as & quot ;:. 3 & quot; three decimal places, & quot;: 8 & quot; occupies 8 character space, etc. in Python, format the output string using the format () function, and the string is a class, available methods;

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;

#-*-Coding: UTF-8-*-# = # File: abop. py # Author: Wendy # Date: #==========================#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.txtMy name is Fred.

The above is the detailed usage of the format () function for formatting the output string in Python. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.