Data structures for Python: Methods in Strings

Source: Internet
Author: User
Tags ord

First, filter string 1, Strip () (1) Remove the line ending invisible character a = ' wejifrow ' Print aprint a.strip () Result: Wejifrowwejifrow (2) strip ([chars]) a = ' **weji** frow** ' Print a.strip (' * ') Results: Weji**frow (3) Lstrip ([chars]) Rstrip ([chars]) s= ' * dwiefi * * ' Print s.lstrip (' * ') print S.rstrip (' * ') Result: Dwiefi * * * dwiefi str1=r ' \n\t andwf\n\t ' Print Str1print str1.strip () print Str1.strip (' \tf\n ') Results: \n\t andwf\n\t\n\t andwf\n\t\n\t andwf\n\t  Two, Case Interchange 1, ASCII value to change Ord CHR2, S1.lower () uppercase converted to lowercase s2.upper () lowercase to uppercase s3.swapcase () Case Interchange 3, the first word of the sentence is converted to uppercase S4.capitalize () The first letter of all words in the sentence need to be capitalized (module method): Import Stringstring.capwords ( S5) or S5.title ()  4, string module import stringstring.uppercasestring.lowercase  Three, 1, S.ljust (width,[ Fillchar]) left-justified s.rjust (width) Right-aligned width definition length Fillchar: What characters are used to complement s= ' 123abc ' Print s.ljust (10, ' * ') results:123abc****  print ' Lilili '. Ljust, ' female '. Ljust (+), ' Max '. Ljust (5), ' Zhouzhou '. Ljust (15) with print '%-15s%-10s%-5s%-15s '% (' Lili ', ' female ', ' all ', ' Zhouzhou ') effect the same as 2, Middle alignment S.centerwidth,[fillchar]) print s.center (15, ' * ') results: *****123abc**** 3,S.zfill (width) s.zfill ==s.rjust (10, ' 0 ') results:0000123abc  Iv. Search 1, S.find (Substr,[start,[end]) SUBSTR the first subscript that appears in the S character start end: Starting at the position where the S string is subscript to start, the position of the subscript end-1 is not found. Returns -1 2, S.index (Substr,[start,[end]]) Description: [Start,[end]] in the case of end, there must be a difference between the Startfind and the index method: find: Return -1index: If not found, an exception is thrown: Valueerror:substring not found use throws an exception Do exception branching (app crashes are often caused by not catching such exceptions) Try:passexcept: 3, S.rfind (Substr,[start[,end])) from the right side of the string to find s= ' Boy boy ' print S.find (' Boy ') print s.rfind (' Boy ') results: 04 4, S.rindex (Substr,[start[,end]]) s= ' Boy Boy ' Print S.rindex (' Boy ') results: 4   V, string substitution 1, s.replace (Oldstr,newstr,[count]) replace Oldstr with Newstr. Count: Replace the number of times without a return value, need S=s.replace ()  2, S.expandtabs ([tabsize]) to replace the tab with a space tabsize: default One tab is replaced by 8 spaces, Replace a TAB key with Tabsize spaces   Six, string cut 1, S.split ([sep[,maxsplit]) Sep: Separates maxsplit with seq string in s string: number of times separated from left to right  2, S.splitlines ([keepends]) separates rows, with newline characters (\ n) keepends=true, preserving line breaks. The default value is False 3, S.rsplit ([Sep[,maxsplit]]) starts from the right of the string   Seven, the list is converted to a string of 1, S.join () is a list of strings stitched into a string  2, S.startwith (SUBSTR) string S is subStrSubstring start, return value TRUE or Falses.endswith (SUBSTR) string s is end with SUBSTR substring, return value TRUE or False 3, S1 in s determine if S1 substring is in string s   Eight, judge string 1 , S.isalpha () s strings are composed of letters, and at least one character is composed of  2, S.isalnum () s strings are composed of letters or numbers, and at least one character is composed of  3, S.isdigit () The characters in the S string are made up of numbers and are composed of at least one character ' 12.23 '. IsDigit () The result is false 4, S.isspace () determines whether the S string is composed of spaces, and at least one character is considered to be a space, as long as it is blank.  5, S.islower () determines whether the letters in the string s (which can contain other characters) are lowercase and have at least one letter true or flase ' A1 '. Islower () results for true 6, S.isupper () Determines whether the letters in the string s (which can contain other characters) are uppercase, and at least one letter  7, S.istitle () determines that the first letter of all words in the string s is uppercase, the other letter is lowercase, and has at least one letter  8, import Stringstring.maketrans (from,to) mapping table, encoding and decoding when used for example: Maptable=string.maketrans (' 123 ', ' abc ') s= ' 111abc123abc ' Print S.translate (maptable) results:aaaabcabcabc  Nine, numeric type conversion int () float () long () import string1, String.atoi (s,a) can also convert octal, Hexadecimal A: Represents the number of s= ' + ' print int (s) print string.atoi (s) Result: 1818print string.atoi (' 011 ', 8) 9print string.atoi (' 0x11 ', 16) 17 2, String.atol (' 11 ') 113, String.atof (' 1.23456 ') 1.23456  10, 1, Ord () letters are converted to the corresponding ASCII value 2, Chr () converts the number to the corresponding character 3, S.encThe ODE ([Encoding[,errors]]) encoding function encodes Unicode to the desired encoding: the encoded type errors: The default value ' strict '. Ignore (Ignore) s.decode () decoding function decoded to Unicode format   

Data structures for Python: Methods in strings

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.