Introduction to the code of the python String module in practical application

Source: Internet
Author: User

The python String module is a computer language that is widely used and has powerful functions in actual operations. However, no one knows how to simply use the python string module, the following describes the actual usage of the python String module.

We recommend that you use the str class instead of the string module. The String module provides common string processing functions. These functions can be found in the str class; some constants provided in the python String module are still very useful.

String member constant:

 
 
  1. ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHI
    JKLMNOPQRSTUVWXYZ' 
  2. ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' 
  3. ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
  4. letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij
    klmnopqrstuvwxyz' 
  5. lowercase = 'abcdefghijklmnopqrstuvwxyz' 
  6. uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
  7. digits = '0123456789' 
  8. hexdigits = '0123456789abcdefABCDEF' 
  9. octdigits = '01234567' 
  10. whitespace = '\t\n\x0b\x0c\r ' 
  11. punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'  
  12. printable = '0123456789abcdefghijklmnopqrstuvwxyz
    ABCDEFGHIJKLMNOPQRSTU...   

Member functions:

 
 
  1. atof(s) -> float  
  2. >>> atof = string.atof  
  3. >>> atof("3.14")  
  4. 3.1400000000000001  
  5. >>> atof("1")                  

Integer that can be processed

 
 
  1. 1.0  
  2. >>> atof("-9")                

It can process plus or minus signs. You cannot add 'F' to the end of a numeric character'

 
 
  1. Traceback (most recent call last):  
  2. File "<pyshell#24>", line 1, in <module> 
  3. atof("3.14f")  
  4. File "C:\Python26\lib\string.py", line 386, in atof  
  5. return _float(s)  
  6. ValueError: invalid literal for float(): 3.14f  
  7. >>> atof("s3.14")                

Throw an exception when an error occurs.

 
 
  1. Traceback (most recent call last):  
  2. File "<pyshell#26>", line 1, in <module> 
  3. atof("s3.14")  
  4. File "C:\Python26\lib\string.py", line 386, in atof  
  5. return _float(s)  
  6. ValueError: invalid literal for float(): s3.14  
  7. atoi(s [,base]) -> int  
  8.  

Converts string S to an integer, and base is 10 by default. The above article introduces the python String module.

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.