Python built-in data structure Basics cognition: string

Source: Internet
Author: User
Tags throw exception

string:An ordered sequence of characters that is a set of characters that uses single, double, and triple quotation marks to enclose a sequence of charactersA string is an immutable objectstrings can be indexed to iteratePython3, the string is the Unicode type

' # single quotation mark inside the double quotation mark direct printing, single quotation marks and special characters need to escape in order to print "" # double quotes inside the single quotation mark direct printing, double quotes inside the double quotation mark and special characters need to escape to print ' ' ' ' # three single quotes inside the single and double quotation marks can be directly printed "" "" "# three double quotes inside single quotes, double quotes and three single quotes can be printed directly in the R" R "" R "" "" in the special characters (\,\r,\n,\t, etc.) are directly printed (implicitly escaped) if the string is not by R (R), or the special characters inside is not escaped, then like \ n , \ t, \ r \ n, \ r This special character represents its ideographic (\ n means newline), as a character string Join Link: "string". Join (iterable)-str# to concatenate an iterative object with a string as a delimiter to return a new string; an element in an iterative object must be a string +-and STR # Two strings added to produce a new list String Segmentation: Split Department # Splits a string into several strings according to the delimiter and returns the list . Split (Sep=none,maxsplit=-1)List of strings# from left to right; Sep Specifies the delimiter, the default is a space (adjacent spaces are treated as a space); Maxsplit Specifies the number of splits, the default-1 is the traversal of the entire string . Rsplit (sep=none,maxsplit=-1)List of strings# from right to left; Sep Specifies the delimiter, which defaults to a space (adjacent spaces are treated as a space); Maxsplit Specifies the number of splits, default-1 for traversing the entire string . Splitlines ([keepends])List of strings # to cut a string by line delimiter, keepends=true to preserve the row delimiter, which is not preserved by default (with split () simpler) Partition Department # Divide the string into headers, separators, and triples in the end . partition (Sep)(Head,sep,tail) # from left to right, encountered delimiter to return header, delimiter, tail three parts of the ternary group; no delimiter found, return the header, two empty elements of the ternary group; Sep split string, must be specified . Rpartition (Sep)(head,sep,tail) # from right to left, encountered a delimiter to return header, delimiter, tail three parts of the ternary group; no delimiter found, return two empty elements, tail of ternary group String Modification: . Replace (Old,new[,count])STR (old and new need to be quoted) #字符串中找到匹配并替换成新子串, returns the new string #count represents the replacement several times, the default all replaces . Strip ([chars]) -> str (character set needs to be quoted) # Removes all characters from the specified character set chars from both ends of the string (as long as any character in the character set of the string is removed) # is not specified, the default is to remove whitespace characters on both sides Lstrip ([chars]) STR # only starts from left Rstrip ([chars]) STR # just start right String Lookup O (n): . Find (Sub[,start[,end]) Int (the sub needs to be quoted) # in the specified interval [start, end), from left to right, find the sub-string sub. Find the first one immediately returns the index, not found return-1 . RFind (Sub[,start[,end]) -int# in the specified interval [start, end], from right to left, finds the substring sub. Find the first one immediately returns the index, not found return-1 index (Sub[,start[,end]])Int (the sub needs to be quoted, used sparingly, may throw an exception) # in the specified interval [start, end), from left to right, find the substring sub. Find the first one immediately returns the index, not found throw exception valueerror Rindex (sub[,start[,end]) Int (the sub needs to be quoted, used sparingly, may throw an exception) # in the specified interval [start, end), from right to left, find the substring sub. Find the first one immediately returns the index, not found throw exception valueerror . Count (Sub[start[,end]]) -int #在指定的区间 [Start,end), left-to-right counts the number of occurrences of a sub-string sub string judgment: startswith (suffix[,start[,end]) -BOOL # in the specified interval [start,end], whether the string is suffix start EndsWith (suffix[,start[,end]) -BOOL # in the specified interval [start,end], whether the string is suffix end is series. Isalnum (), BOOL is a letter and a number. Isalpha () is a letter. Isdecimal () contains only decimal digits. IsDigit () Whether all numbers (0~9). Isidentifier () is not the letter and the underscore start, the others are letters, numbers, underscores. Islower () are all lowercase. Isupper () all uppercase. Isspace () contains only white space characters string Case: . Upper () # Capitalize all strings . Lower () # Convert all strings to lowercase string Formatting: Format Function ' {} '. Format (*args)STR # Prints According to the index location, the default one by one corresponds ' {XXX} '. Format (**kwargs)-Str # follows the dictionary key to print value,xxx represents key :# The delimiter between index and formatted print mode format print mode common:. number #以几位有效数字打印; {:. 2}. Numberf # Print to decimal point precision {:. 2f} # {: 4.2f} means print to decimal 2 Bits and the total string is a total of 4 characters. number% # example: ' {:. 2%} '. Format (7/9) output ' 77.78% ' < # Left justified # {: <10} # 10 means that a total of several characters are printed, the actual character exceeds Then burst > # Right Alignment # {: >10} ^ # Center alignment # {: ^10} *^ # Center align and fill # {:* ^10} Special Example: 1, ' {0:b},{0:x},{0:c},{0 : o} '. Format (97) output ' 1100001,61,a,141 '; 0 means 0, no 0 error B: binary printing; x:16 printing; c:ascii code printing; o: Octal printing 2, >>> Import Datetime
>>> d = Datetime.datetime (2010, 7, 4, 12, 15, 58)
>>> ' {:%y-%m-%d%h:%m:%s} '. Format (d) # time-decoration
' 2010-07-04 12:15:58 '

Python built-in data structure Basics cognition: string

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.