Python string formatting% with format () function IX

Source: Internet
Author: User

This is the content of the December plan, beginning in January 2018 ~

If there is any mistake, please ask for it ~

Formatting this has been mentioned in print a little bit, and the truth is very similar to the C language.

% of the method:

%[(name)][flags][width]. [Precision]typecode

    • (name)       optional, to select the specified key ,
         Print (" Year:% ( Year) S "%{' (Year ': 2018})   
    • flags           optional, available values are:
      • em>+       Right, positive plus, negative before plus minus;
      • -        left-aligned, positive unsigned before negative, plus minus sign;
      • spaces     Right alignment, positive numbers with spaces, negative numbers before plus minus;
      • 0         right-aligned; no sign before positive, negative minus with 0 padding
    • width         optional, occupy width
    • . Precision   Optional, number of digits left after decimal point
    • typecode     required (this should have been seen)

% c

Convert to character (ASCII value, or string of length one)

%r

Prioritize string conversions with the repr () function

%s

Prioritize string conversions with the str () function

%d/%i

Turn signed decimal number

%u

Turn unsigned decimal number

%o

To unsigned octal number

%x /%x

Turns to unsigned hexadecimal number (x/x for the case of converted hexadecimal characters)

%e/%e

Turn scientific notation (e/e control output e/e)

%f/%f

Turns into floating-point numbers (fractional parts are naturally truncated)

%g/%g

%e and%f/%e and%f abbreviations

%

Output% (the format string includes a percent semicolon, so you must use a percentile)

%r and%s are string conversions, so what's the difference?
1String ="hello\tworld\n"2 Print("%s"%string)3 Print("%r"%string)4 the string received by Hello World Str () is user-oriented5 6 'hello\tworld\n'Repr () The resulting string is machine-oriented
And most of the other symbols have been seen, here no longer explain

The Str.format () function replaces the previous "%" with "{}" and ":". Symbol.
[[Fill]align] [Sign] [#] [0] [Width] [,] [. Precision] [Type]
{} by location matching parameters, of course, do not write the default order, you can repeatedly appear
* What does "abc" mean to the function to speak, which is understood here as "abc" = ' A ', ' B ', ' C ' becomes
 1  print  (  " {1},{0},{2}  . Format (*"   ABC     print  ( " {1},{2},{0}  . Format ("   A  ,    b  ,    C   )) 
B,a,cb,c,a
{}  Configuring parameters by name
1 Print ("{a},{b},{c}". Format (a=1,b='s', c=4))
1,s,4
{} configure parameters by subscript
 1  alist=[ " s   ", 2,"  c   "    print  ( " {0[1]}  . Format (alist) 
alist=['s', 2,'C']print('{ 0[1]}'. Format (alist)#2

The above is also known as mapping list and dict

The next step is a series of {: XXXXXX} operations.

Python string formatting% with format () function IX

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.