Python grammar notes (vi)

Source: Internet
Author: User

1, sequence of methods
The sequence in Python contains the list, tuple tuple, and string str.
Built-in functions that can be used for sequences (tables, tuples, strings):

    Len (s) returns: the number of elements in the sequence    min (s) returns: the smallest element in the sequence max (s) returns: the    largest element in the sequence    all (s) returns: true, if all elements are true, any    (s) returns: True if either element is True

Methods that are available for lists and tuples:

    SUM (s) returns: the and # X for all elements in the sequence    are the element values, I is the subscript (the position of the element in the sequence)    s.count (x) returns: The number of occurrences of x in S    s.index (x) returns: X the first occurrence of the subscript in S

Methods that apply only to tables:

    L.extend (L2) adds all elements of the table L2 at the end of Table L    l.append (x) appends the X element to the end of L L.sort (    ) sorts the elements in L l.reverse () to reverse the    elements in L    L.pop ( Return: The last element of table L, and delete the element in table L    del L[i] Delete the element    (all of these methods are performed on the original table and will have an effect on the original table, instead of returning a new table.) )

Methods that apply to strings (although strings are one of the tuples, string classes are some ways that you can change the string.) The essence of these methods is not to manipulate the original string, but to delete the original string, and then create a new string, so it is not inconsistent with the characteristics of the fixed value table. )

    #str为一个字符串, a sub is a substring of str. S is a sequence whose elements are strings.    Width is an integer that describes the width of the newly generated string. Str.count (sub) Returns: The number of times the sub appears in Str str.find (sub) Returns: From the left, find the position of the sub in Str for the first time. If Str does not contain a sub, returns 1 Str.index (sub) return: From the left, find the position of the sub in Str for the first time. If the STR does not contain a sub, the error Str.rfind (sub) is returned: from the right, find the position of the sub in Str for the first time. If Str does not contain a sub, returns 1 Str.rindex (sub) return: From the right, find the position of the sub in Str for the first time. If STR does not contain sub, cite error Str.isalnum () returns: True if all characters are letters or numbers Str.isalpha () returns: True if all characters are letters Str.isdigit () return: true, as All characters are numeric str.istitle () returns: True if all the first letters of the word are uppercase Str.isspace () return: True if all characters are spaces Str.islower () return: True if all characters are lowercase Str.isupper () returns: True if all characters are uppercase letters Str.split ([Sep, [Max]]) return: Starting from the left, dividing str into substrings with a space separator (separator), dividing it to a total of Max times. Returns the resulting substring in a table. You can Str.split (', ') using a comma or other delimiter str.rsplit ([Sep, [Max]]) to return: Starting from the right, dividing str into substrings with a space divider (separator), dividing it in a total of Max times. Returns the resulting substring in a table.    You can Str.rsplit (', ') by using commas or other delimiters str.join (s) to return: The elements in S, with Str as the delimiter, are merged into a string. Str.strip ([Sub]) returns: Removes whitespace at the beginning and end of the string. You can also provide a parameter sub, removing the sub str at the beginning and end of the string. replace (sub, new_sub) returns: Replace the sub Str.capitalize () in Str with a new string new_sub return: Capitalize the first letter of Str Str.lower () Back: Change all str letters to lowercase s Tr.upper () Return: Change all str letters to uppercase Str.swapcase () return: Change the STR capital letter to lowercase, lowercase to uppercase str.title () return: Capitalize the first letter of each word of str (separated by a space) Str.center (    width) returns: A string of width length, placing the original string in the center of the string, and other vacant spaces.    Str.ljust (width) returns: Width-length string, left-aligned the original string into the string, and other vacant spaces. Str.rjust (width) returns: Width-length string, right-aligned the original string into the string, and other vacant spaces.

2. String Formatting
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. For example:
print("I‘m %s. I‘m %d year old" % (‘Vamei‘, 99))
Format characters:

    %s string (displayed with str ())    %r string (displayed with Repr ()    %c single character    %b binary integer    %d decimal integer    %i decimal integer    %o Octal integer    %x hexadecimal integer    %e exponent (base write e)    %e exponent (base write e)%f floating-point number%f floating-point number    with the same    %g exponent (e) or floating-point number (depending on display length)    %G Index (E) or floating point number (depending on display length)    the percent character "%"

The format can be further controlled in the following ways:
%[(name)][flags][width]. [Precision]typecode
(name) is named
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 display widths
Precision indicates the precision after the decimal point

Like what:

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

Reference:

Http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html

Python grammar notes (vi)

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.