[Base] python string method summary

A statement

  0- an ordered sequence of multiple characters;

Second, characteristics

  1, a string data type is immutable   

  2, the string is ordered

Third, the index

  Subscript: 'abcde'

    1, from left to right, 0, 1, 2, ...
    2, right to left, the last character index value -1

    The inverse of the index value of the second character-2 ....

  Slice:
    string [start: stop: step]

    * Start: can take to
    * stop: not take
       care regardless tail
    * start: default from the beginning
    * stop: The default is taken to the end

    N is 1. step
      1, cut from left to right, the positive sequence
      2, start <stop

    2, step is negative
      1, still follow the rules of care regardless of the end of the

      2, from right to left cut
    3, start> stop

      s = 'abcde'

      'abc' --> s[0:3:]

      'cba' --> s[2::-1]

  length

    len (string) # python built-in functions  

  解包
    x, y, z = 'abc'   # x = 'a' z = 'c'

Fourth, the common method string

  1. Find

    find () to find, from the left to return the specified character index in the string value
    rfind () to find, starting from the right to return the specified character index value in a string of
    more than two, if can not find the time, or -1

    index () to find, from the left to return the specified character index value in the string
    rindex () to find, starting from the right to return the specified character index value in a string of
    more than two, if can not find the time, error

    count () calculates the number of times specified character string appearing in

  2. Split
    The splitlines () splits on line breaks, after the split of strings to be placed in a list of
    split (sep, maxsplit)
      Function: a character string in accordance with the cut, the remaining portion not cut
      sep : who accordance cutting, cutting rules   

1 s = 'i love python'
2 s.split(' ') ---> ['i', 'love', 'pyhton']

    maxsplit: the number of cutting; cutting all the default;

    partition (sep)
      function: to split the same, but retain the cut part

  3, replacement
    Replace (Old, new new, COUNT)
    Old: is replaced by a string of
    new: the replacement string
    count: the number of replacements, the default replace all

    translate ()
      function: in accordance with the control relationship alternative
      parameters: control relationship
        str.maketrans (character string to be replaced, replace the string)

      Where: replaced and replaced with the character of each one correspondence

        m = str.maketrans ( 'beautiful', 'beautiful')

      'Snow really beautiful' .translate (m) -> snow is so beautiful '

  4, modified
    center center
    ljust Left
    rjust right-aligned
    zfill from the left with 0 fill
    strip to remove leading and trailing spaces, you can also remove the specified character
    lstrip removal left a space, you can also remove the specified character
    rstrip the right to remove spaces, ......

  5、format

    (1) by filling contents position parameter
    (2) filling the contents of keyword parameter
    (3) is filled with the format
      {:} ^ 8 centrally length 8, a space filled with
      {: * <10} Left length 10, with a * filling

    (4) accuracy and binary
      {: .3f} After three decimal places
      {: o} to convert the decimal number to octal
      {: x} to convert decimal to hexadecimal
      {: b} converts the decimal number binary number  

  6, a string formatted

    '% D ...% f ...% s'% (integer data, float data, string data)

  7, a string deformation
    upper change uppercase
    lower variable lowercase
    swapcase case interchangeable
    title of the first character of each word capitalized
    capitialize the first character of the entire string uppercase
    expandtabs the \ t ---> spaces, a total of eight characters

  8, the string is determined
    isalnum whether all letters or numbers
    isdigit digital .......... .....
    the isalpha letter ..... ..........
    isupper letter whether the part is capitalized
    islower whether lowercase letters are part
    istitle capitalize the first letter of each word whether
    isspace whether entirely of blank characters
    startswith whether to begin with ....
    whether endswith to end ....

  9, python two built-in function
    dir () to view the object of all methods
    help () function to use to view, documentation

Third, the escape character

  Changing the original character meaning: Symbol: \, eg, \ n, \ t, \ b, \ w

  Original string: r '' to maintain the original character string of the meaning of each character

Guess you like

Origin www.cnblogs.com/Tree0108/p/12090436.html