python String模块在实际应用的代码介绍


python String模块是一种应用相当广泛,在实际操作中功能十分强大的计算机语言,但是懂得如何简捷的运用python string模块的这一语言的人却不占大多数,以下的内容就是对python String模块在实际运用的解析。

推荐使用str类,而不是string模块:String模块提供了常用的字符串处理函数;这些函数通常可以在str类中找到对应;python String模块内提供的某些常量还是非常有用的。

string成员常量:

  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...  
     

成员函数:

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

可以处理整数

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

可以处理正负号不可以在数字字符尾端加‘f’

  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")                

遇到错误输入,抛出异常

  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.  

转换字符串S为整数,base默认为10。以上的文章就是对python String模块的相关介绍。

相关内容

    暂无相关文章

评论关闭