"Python" string function

Source: Internet
Author: User

Constants in the 1.String module:

String.digits: Digital 0~9

String.letters: All Letters (case)

String.lowercase: All lowercase letters

String.printable: A string that can print characters

String.punctuation: All punctuation

String.uppercase: All uppercase letters

Import string>>> string.uppercase ' abcdefghijklmnopqrstuvwxyz ' >>> string.lowercase ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' >>> string.digits ' 0123456789 ' >>> string.letters ' Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ' >>> string.printable ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! " #$%&\ ' () *+,-./:;<=>[email protected][\\]^_ ' {|} ~ \t\n\r\x0b\x0c ' >>> string.punctuation '! " #$%&\ ' () *+,-./:;<=>[email protected][\\]^_ ' {|} ~

The 2.find function queries substrings in a longer string, returns the leftmost index where the substring is located, and does not find return-1

>>> title = "Monty Python ' s Flying Circus"  >>> title.find (' Monty ')  0  >>> Title.find (' Monty ')  -1  can choose Start point and end point >>> title.find (' Python ')  6  >>> title.find (' Python ', 3)  6  >>> title.find (' Python ', 3, Ten)  -1  

The 3.join function adds an element to the queue (only works on a string, returns a modified string, but the original string does not change), its inverse function splits the string into a sequence for the Split,split function, returns the sequence, and the original string does not change

>>> seq = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']  >>> Sep = ' + '  >>> sep.join (seq)  ' 1+2+3+4+5 ' 
   >>> seq  [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']  >>> dirs = ', ' usr ', ' bin ', ' env '  >>> '/'. Join (dirs)  ' /usr/bin/env '  >>> print ' C: ' + ' \ \ '. Join (dirs)  C:\usr\bin\env  >>> word = ' 1+2+3+4+5 '  >>> word.split (' + ')  [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']  >>> word  ' 1+2+3+4+5 '  

4.lower function returns the lowercase master of a string

 

>>> ' fafdawdfawedwed '. Lower ()  ' fafdawdfawedwed '  extension: Title function: Capital letter, other lowercase >>> "That's all" Folks ". Title ()  

The 5.replace function returns a string where all occurrences of a string have been replaced, and the original string does not change

>>> Word = ' This was a test '  >>> word.replace (' is ', ' EEZ ')  ' theez eez a test '  >>> wo Rd  ' This is a test

6.strip function to remove strings of both sides (excluding interior) spaces, the original sequence is unchanged

>>> Word = ' This was   test    '  >>> word.strip ()  ' This is test '  >>> Word  ' This   is Test    '  can add parameters in strip () to remove the specified character that you want to remove >>> ' * * *  SPAM  * for  *  everyone!!!  '. Strip (' * ')  '  SPAM  *  for  *  everyone!!!  '  >>> ' * * *  SPAM  * for  *  everyone!!!  '. Strip (' * ')  ' SPAM  *  for  *  everyone!!! '  

  

"Python" string function

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.