Python: String Functions

Source: Internet
Author: User

Constants in the 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

[HTML]View Plain Copy
  1. >>> Import string
  2. >>> String.digits
  3. ' 0123456789 '
  4. >>> String.letters
  5. ' Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '
  6. >>> String.lowercase
  7. ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '
  8. >>> String.printable
  9. ' 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz! ' #$%&\ ' () *+,-./:; <=>[email protected][\\]^_ ' {|} ~ \t\n\r\x0b\x0c '
  10. >>> String.punctuation
  11. ‘!" #$%&\ ' () *+,-./:; <=>[email protected][\\]^_ ' {|} ~ '
  12. >>> String.uppercase
  13. ' ABCDEFGHIJKLMNOPQRSTUVWXYZ '

1. Find function

Queries a substring in a longer string, returns the leftmost index where the substring is located, and no return-1 is found

[HTML]View Plain Copy
    1. >>> title = "Monty Python ' s Flying Circus"
    2. >>> Title.find (' Monty ')
    3. 0
    4. >>> Title.find (' Monty ')
    5. -1
You can select a starting point and an end point

[HTML]View Plain Copy
    1. >>> Title.find (' Python ')
    2. 6
    3. >>> Title.find (' Python ', 3)
    4. 6
    5. >>> Title.find (' Python ', 3, ten)
    6. -1

2. Join function

Adding elements to a queue (only works on strings, returns a modified string, but the original string does not change)

[HTML]View Plain Copy
  1. >> seq = [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
  2. >>> Sep = ' + '
  3. >>> Sep.join (seq)
  4. ' 1+2+3+4+5 '
  5. >>> Seq
  6. [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
  7. >>> dirs = ', ' usr ', ' bin ', ' env '
  8. >>> '/'. Join (dirs)
  9. '/usr/bin/env '
  10. >>> Print ' C: ' + ' \ \ '. Join (dirs)
  11. C:\usr\bin\env

Inverse method: Split function

Splits a string into a sequence, returns the sequence, and the original string does not change

[HTML]View Plain Copy
    1. >>> Word = ' 1+2+3+4+5 '
    2. >>> Word.split (' + ')
    3. [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
    4. >>> Word
    5. ' 1+2+3+4+5 '

3. Lower function

Returns the lowercase master of a string

[HTML]View Plain Copy
    1. >>> ' fafdawdfawedwed '. Lower ()
    2. ' Fafdawdfawedwed '
Extended:

Title function: Capital Letter, other lowercase

[HTML]View Plain Copy
    1. >>> "That's All Folks". Title ()
    2. "That's all folks"
Capwords function: function as above, function in string module

[HTML]View Plain Copy
    1. >>> Import string
    2. >>> String.capwords ("That's All Folks")
    3. "That's all folks"

4. Replace function

Returns the string from which all occurrences of a string are replaced, without changing the original string

[HTML]View Plain Copy
    1. >>> Word = ' This is a test '
    2. >>> Word.replace (' Is ', ' EEZ ')
    3. ' Theez eez a test '
    4. >>> Word
    5. ' This is a test '

Maketrans function: function above, conversion table in string, total 256 items, function accepts 2 equal length string, each character in the first string is replaced by character in the corresponding position in the second string

Maketrans is similar to a rule that is often combined with translate to complete a string substitution that cannot be accomplished by some ordinary function

[HTML]View Plain Copy
    1. >> > from string import maketrans  
    2. >>> table =  maketrans (' cs ',  ' KZ ')   
    3. >>> len (table)    
    4. 256  
    5. > >> table[97:123]  
    6. ' abkdefghijklmnopqrztuvwxyz '   
    7. >>> maketrans (', ') [ 97:123]  
    8. ' abcdefghijklmnopqrstuvwxyz '   

Translate function: function as above, but can only handle a single character, there are 2 parameters, the first is a replacement, the second is to delete

Example: Table in a table successor Maketrans

[HTML]View Plain Copy
    1. >>> ' This was an incredible test '. Translate (table)
    2. ' Thiz iz an inkredible tezt '
    3. >>> ' This was an incredible test '. Translate (table, ')
    4. ' Thizizaninkredibletezt '

5. Strip function

Remove strings from both sides (excluding inner) spaces, the original sequence is unchanged

[HTML]View Plain Copy
    1. >>> Word = ' This is test '
    2. >>> Word.strip ()
    3. ' This is test '
    4. >>> Word
    5. ' This is test '
You can add a parameter to the strip () to remove the specified character that you want to remove

[HTML]View Plain Copy
  1. >>> ' * * * * SPAM * for * everyone!!! '. Strip (' * ')
  2. ' SPAM * for * everyone!!! ‘
  3. >>> ' * * * * SPAM * for * everyone!!! '. Strip (' * ')
  4. ' SPAM * for * everyone!!! '
  5. >>> ' * * * * SPAM * for * everyone!!!  '. Strip (' *! ')
  6. ' SPAM * for * everyone '

Python: String Functions

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.