Format output string in Python using format () function usage

Source: Internet
Author: User
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.

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.