Print/format string formatting instances in Python

Source: Internet
Author: User

the Python string is formatted using the "character% format 1% format 2 characters"% (variable 1, variable 2), and the% format represents the type of the accepted variable. Simple examples of use are as follows

# Example: String formatting
Name = ' 17jo '
print ' www.%s.com '%name
>> www.111cn.net

Name = ' 17jo '
Zone = ' com '
print ' www.%s.%s '% (name,zone)
>> www.111cn.net

The string is formatted with a different format symbol after the percent sign, representing the different types to be converted, and the specific representation symbol is shown below.

Format symbol representation type
%s string
%d/%i decimal Integer
%u decimal integer
%o Eight-binary integers
%x/%x hexadecimal integer
%e/%e Scientific counting
%f/%f floating Point
% OUTPUT%

When the format symbol is a number, it can be preceded by an amount and a fill-in position such as:%[0][total number of digits [.] [Decimal place] to set the style to be converted, using the following method:

# example: Numeric formatting
nyear = 2018
Nmonth = 8
Nday = 18
# formatted date%02d number to two-bit integer vacancy fill 0
print '%04d-%02d-%02d '% (nyear,nmonth,nday)
>> 2018-08-18 # Output results

Fvalue = 8.123
print '%06.2f '%fvalue # reserved 2-bit decimal floating-point with a width of 6
>> 008.12 # Output

print '%d '%10 # output decimal
>> 10
print '%o '%10 # output octal
>> 12
print '%02x '%10 # output two-bit hex, letter lowercase blank complement 0
>> 0a
print '%04x '%10 # output four-bit hex, letter capital Vacancy 0
>> 000A
print '%.2e '%1.2888 # Output floating-point type with scientific notation reserved 2 decimal places
>> 1.29e+00

In this way, we have shown how to replace the specified field. We can also make precise formatting by adding a colon after the format indicator. For example:

Python:format

# Field 0:left justify, pad to characters
# Field 1:right Justify, pad to 6 characters
FMT = ' {0:15} ${1:>6} '

Fmt.format (' registration ', +)
' Registration $35 '

Fmt.format (' Tutorial ', +)
' Tutorial $50 '

Fmt.format (' Banquet ', ()
' Banquet $125 '

Formatting indicators can be referenced by nesting.

FMT = ' {0:{1}} '


width = 15
Fmt.format (' Invoice #1234 ', width)
' Invoice #1234 '

width = 35
Fmt.format (' Invoice #1234 ', width)
' Invoice #1234 '

You can specify the alignment of the string that you want to length.
Effect characters:
< (default) align Left
> Right Alignment
^ Middle Alignment
= (for numbers only) after the decimal point

Formatting indicators can contain a presentation type to control formatting. For example, a floating-point number can be formatted as a general format or represented by a power.

>>> ' {0:g} '. Format (3.75)
' 3.75 '
>>> ' {0:e} '. Format (3.75)
' 3.750000e+00 '

There are many types of impressions. The complete list is in the 2.6 document. Some examples are listed here.

' 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. Print numbers in scientific notation. Use ' e ' to indicate 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.
'% '-percent. Multiply the value by 100 and then print in fixed-point (' F ') format, followed by a percent semicolon.

Classes and types can define a __format__ () method to control how they are formatted. It will accept a format indicator as an argument:

def __format__ (self, Format_spec):
If Isinstance (Format_spec, Unicode):
return Unicode (self)
Else
Return str (self)

There is also a built-in format () method that can be used to format a value. It invokes the __format__ () method of the type and passes the format indicator as a parameter.

>>> format (75.6564, '. 2f ')
' 75.66 '

Print/format string formatting instances in Python

Related Article

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.