Python Learning Summary 6: String formatting operations and summary of methods

Source: Internet
Author: User

1. Format operation (%)

Python has a built-in operation for formatting strings.

Template

When formatting a string, Python uses a string as a template. There are formatting characters in the template that reserve locations for real values and describe the format in which real values should be rendered.

Python uses a tuple to pass multiple values to the template, each of which corresponds to a format character.

" I ' m%s. I ' m%dyear old" % ('vamei')print(a)

in the example above,"I ' m%s." I ' m%d year old "is a template . %s is the first format character , which represents a string. %d is the second format character, which represents an integer. (' Vamei ', 99) Two elements ' Vamei ' and 99 are substituted for the true values of%s and%d. Between the template and the tuple, there is a% number separated, which represents the format operation. The entire "I ' m%s. I ' m%d year old "% (' Vamei ', 99) actually forms a string expression. We can assign it to variable a as a normal string.

In addition, you can use a dictionary to pass real values

Print ("I ' m% (name) s.") I ' m% (age) dyear old" % {'name':'vamei'  Age': 99})

As you can see, we have named two of the format characters . The name is used () enclosed. A key for each named corresponding dictionary.

Format character

The format character reserves the location for the real value and controls the format of the display. A format character can contain a type code that controls the type of display, as follows:

     %s    strings (with STR () display)
     %r     string (display with repr ()
      %c    single character
      %b    binary integer
     %d    decimal integer
     %i      Decimal integer
     %o     octal integer
     %x    hexadecimal integer
     %e     Index (Base write e)
     %e    Index (base write e)
      %f    floating point
     %f    floating point number, same as on
     %g    Index (e) or floating point number (depending on display length)
     %g     Index (E) or floating point number (depending on display length)
     %%    character "%"

The format can be further controlled in the following ways:
%[(name)][flags][width]. [Precision]typecode

Name: for name
Flags: can have +,-, ' or 0. + Indicates right alignment. -Indicates left alignment. ' is a space that fills a space on the left side of a positive number to align with a negative number. 0 means using 0 padding.
Width: Indicates the width of the display
Precision: Indicates the precision after the decimal point

Print ("%+10x" %) Print ("%04d" % 5) Print ("%6.3f" % 2.3)

2. Method of formatting

Str1.zfill (width)
Parameter Width--Specifies the length of the string. The original string is right-aligned, preceded by 0, and returns a string of the specified length.

>>> ' Zfill (5)
' 00012 '
>>> ' -3.14 '. Zfill (7)
'-003.14 '
>>> ' 3.14159265359 '. Zfill (5)
' 3.14159265359 '

str1. Rjust (width) to the right, fill in the left space

Str1.ljust (width) to the left, fill the space on the right

Str1.center (width) to center the string, fill in the Left and right spaces

" 123 ". Rjust (5)  "123". Ljust (5"123 "

Summary:the built-in % operator in Python can be used to format string operations and control the rendering format of strings. There are other ways to format strings in Python, but the use of the% operator is most convenient.

Python Learning Summary 6: String formatting operations and summary of methods

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.