(Common operation of str) Python learning content the next day

Source: Internet
Author: User
Tags first string

Common Operations for STR
  1. Slice, index, number of characters
     name = "  AITG   " #  Span style= "COLOR: #008000" > output t  print  (name[2 ])  #   output ait  print  (Name[0:3 #   output 4  print  (Len ( Name) 

     

  2. First letter Case
    " AITG " Print (Name.capitalize ())

  3. All Letters lowercase
    " AITG " Print (Name.capitalize ()) # Output: AITG Print (Name.lower ()) # Output: AITG

  4. Set the width and center the content
     name =  " AITG  "  print  (Name.center ( " #   "  #   output: # # # aitg###  

     

  5. go to the string and look for the number of occurrences of the subsequence
     name = Span style= "COLOR: #800000" > " AITGAITGAITG   " print  (Name.count ( " a   "  #   output 3 times  print  (Name.count ( " a  " , 0,7 #   find 0-7-bit (does not contain 7th digit) occurrences  #   output 2 times  

     

  6. find string beginning with | end
     name =  " AITG  "  Span style= "COLOR: #000000" > Find the character ending with  print  (Name.endswith ( " g  "   #   Span style= "COLOR: #0000ff" >print  (Name.startswith ( " a  "   #   output result: False or True  

     

  7. string according to \ t segmentation
     name = "  name\tmail\tpass\nname\tmail\tpass\nname\tmail\tpass   " print  (Name.expandtabs (10 #   by \ t segmentation  #   output  #   name Mail pass  #   name Mail pass  #   name Mail pass  

     

  8. Find where a string first appears
    " AITG " Print (Name.find ("t")) # look back from the beginning, find the first string, the output gets the position # Output 2
    #未找到输出结果-1

  9. format the string.
    " I am {a}, age {b} " # Specifies the incoming Print " AITG ", B = +))# Dictionary method incoming print(Name.format_map ({"A  "'aitg'"b": 27}))

  10. Whether the string contains letters and numbers
     name =  " AITG  "  Span style= "COLOR: #008000" >#   output True  print   (Name.isalnum ())  #   output False  print  ( " Span style= "COLOR: #800000" >ait g\t   ". Isalnum ()) 

     

  11.  name = "  AITG   "  output True  print   ( Name.isalpha ())  print  ( "  yards farming is crazy   " .isalpha () Output False  print  ( " 1234  " . Isalpha ()) 

     

  12. current input is a number
      #   support 10-digit judgment  print  ("  1234324   " .isdecimal ()) supports 10 binary and 10 decimal  print  ("  1234①⑨   " .isdigit ()) supports 10 digits, 10 decimal, Kanji digit  print  ( "  twos 1234①⑨  "  .isnumeric ())  #   output True  

     

  13. Whether there are non-visible characters/t/n
    " aitg\t\n " Print (Name.isprintable ()) # output False does not appear true

  14. Determine if all are spaces
    "   " Print (Name.isspace ()) # all are spaces return true no person False

  15.  name = "  my name is AITG   "" Span style= "COLOR: #008000" >#   #   output False  print   (Name.istitle ())  #   Convert to title format  #   output My Name is AITG  print  (Name.title ()) 

     

  16. Stitching each element in a string according to the specified delimiter
    " AITG " Print ("_". Join (name)) # Output A_i_t_g

  17. determine if all are case and converted to uppercase and lowercase letters
    #determine if all lowercase letters arePrint('AITG'. Islower ())#determine if the letters are all uppercasePrint('AITG'. Isupper ())#Convert all letters to lowercasePrint('AITG'. Lower ())#convert all letters to uppercasePrint('AITG'. Upper ())

  18. remove specified string
      #   You can specify the removal character, with a limited number of matches.  name =   AITG  "  #   default remove space to the left \ n  print   (Name.lstrip ())  #   The default removes the space on the right \ \ n  print   (Name.rstrip ())  #   default removes spaces on both sides \ \ n  print  (Name.strip ()) 

     

  19. corresponding relationship substitution
    " AITG " Print (Name.replace ("g""g")) # Output AITG

  20. Split String
    Name ="AITGAITG"#The default is divided into three parts#from left to rightPrint(Name.partition ("T"))#from right to leftPrint(Name.rpartition ("T"))#split the specified number from left to rightPrint(Name.split ("T", 1))
  21. Case conversion (uppercase and lowercase conversions in letters)
    " AITGAITG " Print (Name.swapcase ()) # Output AITGAITG

(Common operation of str) Python learning content the next day

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.