Python Full Stack Learning summary update string: Method of String formatting

Source: Internet
Author: User

100 semicolon Mode

"""percent-semicolon mode%[(name)][flags][width]. [Precision]typecode (name) optional, used to select the specified keyflags optional, the values available for selection are: + Right-justified, positive before plus, negative numbers before plus minus;-left alignment; positive sign, negative before plus minus , white space right alignment, positive number before adding a space, negative number before plus minus, 0 right, positive before the sign, negative number before the minus sign; with 0 padding width is optional, occupy the breadth. Precision optional, the number of digits retained after the decimal point TypeCode required S, obtained Takes the return value of the __str__ method of the passed-in object, formats it to the specified position R, gets the return value of the __repr__ method of the incoming object, and formats it to the specified position C, integer: Converts the number to its Unicode corresponding value, the 10 binary range is 0 <= i <= 1114111 (PY27 only supports 0-255); character: Adds a character to a specified position o, converts an integer to an octal representation, formats it to a specified position x, converts an integer to a hexadecimal representation, and formats it to a specified position d, converting an integer, floating-point number to a decimal representation, and formats it to the specified position E, converts integers, floating-point numbers into scientific notation, and formats them into a specified position (lowercase e) e, converts integers, floating-point numbers into scientific notation, and formats them into a specified position (capital E) F, converting integers, floating-point numbers to floating-point numbers, and format it to the specified location (the default is 6 digits after the decimal point) F, ditto G, auto-adjust the integer, floating-point number conversion to float or scientific notation (more than 6 digits in scientific notation), and format it to the specified position (if the scientific count is E;) G, Auto-adjust the integer, floating-point numbers into float or scientific notation (more than 6 digits in scientific notation), and format it to a specified position (if the scientific count is e;)%, when there is a format flag in the string, you need to use a percent Represents a percent semicolon note: The format of a percent semicolon in Python does not have a way of automatically converting integers to binary representations"""msg='I am%s My age is%s'%('Tom', 18)#string concatenationmsg ='I am%s My age is%d'%('Tom', 18)#use D to represent numbersmsg ='I am% (name) s my age are% (age) S'%{'name':'Liu',' Age': 19}#through the dictionary of the situation splicingmsg ='value:%.3f'%1.56789#Stitching floating point numbermsg ='value:% (value). 4f'%{'value': 9.87654}#floating-point numbers are spliced in a dictionary formmsg ='value:%.2f%%'%50.7823#Print percent signPrint(msg)

Two format modes

"""format mode [[Fill]align][sign][#][0][width][,][.precision][type]fill] optional white space filled character align optional alignment (required with width use) <, content left-aligned, content right-aligned (default) =, content right-aligned, placing symbols to the left of the fill character, and only valid for numeric types. Even if: symbol + padding + Number ^, the content center sign "optional" has unsigned number +, plus home plus, minus negative;-, plus constant, minus negative; # "optional" for binary, octal, hexadecimal, if added #, it will show  0b/0o/0x, otherwise not displayed, "optional" adds a delimiter to the number, such as: 1,000,000width the width of the "optional" format bit. Precision "optional" decimal digit reserved Precision type "Optional" format type incoming " String Type "parameter s, format string type data blank, unspecified type, default is None, with S passed" integer type "parameter B, the 10 binary integer is automatically converted to 2 binary representation and then formatted C, the 10 binary integer is automatically converted to its corresponding Unicode character D, Decimal integer o, the 10-binary integer is automatically converted to 8 binary representation and then formatted; x, the 10-decimal integer is automatically converted to a 16-binary representation and then formatted (lowercase x) x, the 10-decimal integer is automatically converted to a 16-binary representation and then formatted (uppercase X) into the "float or decimal type" parameter E, Converted to scientific notation (lowercase e) is represented, then formatted; E, converted to scientific notation (uppercase E) is represented, then formatted; F, converted to floating point (6 bits after the default decimal point) is represented, then formatted, F, converted to floating point (the default is 6 digits after the decimal point) representation, and then formatted; Automatically toggle G in E and F, automatically toggle% in E and F, display percentages (6 digits after the decimal point by default)"""Info="I am {},my hobby is {},my age:{}". Format ('Tom','Play',' -')#format in orderinfo ="I am {3},my hobby is {1},my age:{2}". Format ('Tom','Play',' -','Qilvzhuiche')#tuple is passed, string is formatted according to the tuple indexinfo ="I am {name},my hobby is {hobby},my age:{age}". Format (name ='Tom', hobby ='Play', age = 28)#format by Assignmentinfo ="I am {name},my hobby is {hobby},my age:{age}". Format (**{"name":'Tom',"Hobby":'Play',' Age': 30})#in the form of a dress dictionary assignmentInfo="I am {0[0]}, age {0[1]}, really {1[2]}". format ([1, 2, 3], [11, 22, 33])#to value the way the list is indexed by the partyinfo ="I am {: s}, age {:d}, Money {: F}". Format ("Seven", 18, 88888.1)#tuple-specified form formattinginfo ="numbers: {: b},{:o},{:d},{:x},{:x}, {:%}". Format (15, 15, 15, 15, 15, 15.87623, 2) Info="numbers: {0:b},{0:o},{0:d},{0:x},{0:x}, {0:%}". Format (15) Info="numbers: {num:b},{num:o},{num:d},{num:x},{num:x}, {num:%}". Format (num=15)Print(info)

Python Full Stack Learning summary update string: Method of String formatting

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.