7 String

7.4 Common string operations

  • Python string objects provide a large number of methods for string detection, replacement, and typesetting operations, as well as a large number of built-in functions and operators that also support string operations.
  • Note that when using it, the string object is immutable,
    • Therefore, the methods provided by the string object related to the string "modification" are to return the modified new string, and do not make any modification to the original string, without exception.

7.4.1 find()、rfind()、 index()、 rindex()、 count()

  • find () and rfind () find the position of the first and last occurrence of a string in the specified range of another string (the default is the entire string), if it does not exist, it returns -1;
  • index () and rindex () return the position of the first and last occurrence of a string in the specified range of another string, and throw an exception if it does not exist;
  • count () returns the number of occurrences of a string in another string,
    or 0 if it does not exist.

Insert picture description here

7.4.2 split、 rsplit、 partition、 rpartition

  • They are used to separate the specified characters as a delimiter, starting from the left and right ends of the string into multiple strings, and return to the list.

Insert picture description here

  • If no delimiter is specified, the consecutive occurrence of any blank symbols (including spaces, line breaks, tabs, etc.) in the string will be considered as delimiters

Insert picture description here

Published 589 original articles · 300 praises · 80,000 + views

Guess you like

Origin blog.csdn.net/zhoutianzi12/article/details/105585920