[Go] Powerful Python string parsing

Source: Internet
Author: User

1.python strings usually have single quotes (' ... '), double quotation marks ("..."), three quotation marks ("" "..." "") or ("..."), or ("."), surrounded by a string of three quotes that can consist of multiple lines, generally representing a large segment of the narrative string. There is no difference in use, but double quotes and three quotation marks ("" "" "" "" "" "") can contain single quotes, and three quotation marks ("...") can contain double quotes without escaping. 2. Use (\) to escape special characters, such as (\), ('), ("). 3. Common string built-in function 1) str.count ()//Returns the number of occurrences of a substring in the string 2) Str.find ()//Returns a substring that appears at the beginning of the string 3) Str.lower ()//converts the string to lowercase 4) str. Upper ()//to uppercase 5) str.Split ()//split string, returns a list of strings, separated by a space by default6) Len (str)//return string length For example: >>> str = ' Hello, world ' >>> str.count (' o ') >>> 2 >>&G T Str.find (' lo ') >>> 3 >>> str.lower () >>> ' Hello, World ' >>> str.upper () >>& Gt ' HELLO, World ' >>> str.Split ()>>> [' Hello, ', ' World '] >>> str.split (', ')>>> [' Hello ', ' world '] >>> len (str) >>> + >>> str >>> ' Hello, World ' None of these actions will change the string itself! 4. Regular expressions, re module import re common functions: 1) Compile ()://compile regular expression string into regular Re object 2) search ()//Match regular expression in target string 3) match ()//from Target The first character of the string begins to match the regular expression search and match match matches successfully returnedMatchobject object, failed to return none
>>> p = re.compile (' abc ') >>> p.search (' zabcy ') <_sre. Sre_match object at 0x2a95659030> is also possible without first compiling the regular re objects, which can also be: >>> re.search (' abc ', ' Xabcy ') <_sre. Sre_match object at 0x2a95659098> Compile can also add some flag bits, for example: RE. I (re. IGNORECASE) Ignore case >>> p = re.compile (' abc ') >>> print p.search (' xabcy ') None >>> p = re.compile ( ' ABC ', Re. I) >>> print p.search (' xabcy ') <_sre. The Sre_match object at 0x2a9565a098> Search and Match differences are shown in the following example: >>> p = re.compile (' abc ') >>> Print P.search ( ' Xxxabcyyy ')
<_sre. Sre_match Object at0x2a95659030> >>>print p.match (' xxxabcyyy ') None >>>print p.match (' abcyyy ') <_sre. Sre_match Object at0x2a95659098>
4)split ()//similar string built-in function split () The difference is: built-inSplit () to determine the string split, while the regular split function splits the word with regular expressions

For example: Split with a space (one or more spaces):

>>>p.split (' ab C d ')
[' A ', ' B ', ' C ', ' d ']

The result of the built-in split split is:

>>> ' a B c d '. Split (")
[' A ', ' B ', ', ', ', ' C ', ', ' d ']

5) FindAll ()//Returns the list of all substrings in the target string that match the regular expression

>>>p = Re.compile (' ^ ([a-z]{2}):([1-9]{3}):(. +) $ ')
>>>p.findall (' As:123:a12 ')
[(' as ', ' 123 ', ' A12 ')]

The substring of the previous example is 3 parentheses, respectively: ' [a-z]{2} ', ' [1-9]{3} ', '. + ', respectively, by AS, 123, A12, note that this returns a one-dimensional list of matching string tuples.

More commonly used regular functions, please refer to the Python Manual for more usage. 5. String and number conversion, string module import string String.atoi (Str[,base])//base as an optional parameter, indicating that the conversion of characters to the number of binary type to a string can be simple, directly with STR () 6. Character and ASCII conversionchar-> ASCII Ord ()
ASCII->char chr ()

[Go] Powerful Python string parsing

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.