python string模块

来源:互联网 发布:python 画图 线条粗细 编辑:程序博客网 时间:2024/05/16 06:12
   1: #!/usr/bin/python
   2: import string
   3: funcs=[]
   4: vars=[]
   5: for element in dir(string):
   6:   name = "string.%s" %element                                                                                                         
   7:   if callable(eval(name)):
   8:     funcs.append(name)
   9:   else:
  10:     vars.append(name)

结果如下:

vars:  ['string.__builtins__', 'string.__doc__', 'string.__file__', 'string.__name__', 'string.__package__', 'string._idmap', 'string._idmapL', 'string._re', 'string.ascii_letters', 'string.ascii_lowercase', 'string.ascii_uppercase', 'string.digits', 'string.hexdigits', 'string.letters', 'string.lowercase', 'string.octdigits', 'string.printable', 'string.punctuation', 'string.uppercase', 'string.whitespace']
funcs: ['string.Formatter', 'string.Template', 'string._TemplateMetaclass', 'string._float', 'string._int', 'string._long', 'string._multimap', 'string.atof', 'string.atof_error', 'string.atoi', 'string.atoi_error', 'string.atol', 'string.atol_error', 'string.capitalize', 'string.capwords', 'string.center', 'string.count', 'string.expandtabs', 'string.find', 'string.index', 'string.index_error', 'string.join', 'string.joinfields', 'string.ljust', 'string.lower', 'string.lstrip', 'string.maketrans', 'string.replace', 'string.rfind', 'string.rindex', 'string.rjust', 'string.rsplit', 'string.rstrip', 'string.split', 'string.splitfields', 'string.strip', 'string.swapcase', 'string.translate', 'string.upper', 'string.zfill']

String的函数

函数 功能描述 实例 atof string.atof(s) 如果是一个浮点数的字符串表示,则转换为浮点数。
否则异常。

>>> string.atof('1.233')
1.2330000000000001
>>> string.atof('1.233s')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/string.py", line 388, in atof
    return _float(s)
ValueError: invalid literal for float(): 1.233s

capitalize string.capitalize(s)返回字符串s的一个副本,这个副本的第一个字符大写

>>> s='hello world'
>>> string.capitalize(s)
'Hello world'

capwords string.capwords(s)每个单词的首字母大写。

>>> s='hello world'
>>> string.capwords(s)
'Hello World'

>>> s='hello, world'
>>> string.capwords(s)
'Hello, World'

center 通过左右填充达到指定长度,并使得给定字符串在中间位置。不会截断。
如果设定长度小于字符串长度,返回字符串。

>>>str = 'test'
>>>print str.center(1)
>>>print str.center(10) 

test
   test

count 包含的字符串的个数

>>> str = 'test'
>>> str.count('t')
2
>>> str.count('a')
0

expandtabs 调整tab的宽度。

>>> str = 'test\ta'
>>> print str
test    a
>>> str.expandtabs(2)
'test  a'

>>> str.expandtabs(0)
'testa'

find 从左侧开始找到的位置的下标(即从左往右第一个match的),找不到返回-1

>>> str = 'test'
>>> str.find('se')
-1
>>> str.find('es')
1

index 类似于index,但是找不到会抛出异常

>>> str = 'test'
>>> str.index('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> str.index('e')
1

join 将list或元组拼接成字符串,默认使用空格拼接

>>> str1 = 'test'
>>> str1.join(' ')
' '
>>> str1.join('world')
'wtestotestrtestltestd'
>>> string.join('world')
'w o r l d'

joinfields 和join几乎等价。注意string对象没有该方法

string.joinfields('world')
'w o r l d'
>>> str1.joinfields('world')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'joinfields'

ljust 输出width个字符,不足使用空格。左对齐输出。

>>> str1.ljust(1)
'test'
>>> str1.ljust(2)
'test'
>>> str1.ljust(3)
'test'
>>> str1.ljust(6)
'test  '
>>> string.ljust(str1, 2)
'test'
>>> string.ljust(str1, 6)
'test  '

lower 小写表示

>>> str = 'Hello World'
>>> str.lower()
'hello world'

lstrip 去除左侧的,默认为空白符

>>> str = '  Hello World  '
>>> str.lstrip()
'Hello World  '
>>> str = '  \tHello World  '
>>> str.lstrip()
'Hello World  '
>>> str.lstrip('e')
'  \tHello World  '
>>> str = 'eeee  \tHello World  '
>>> str.lstrip('e')
'  \tHello World  '

maketrans 返回一个256个字符组成的翻译表,其中from中的字符被一一对应地转换成to,所以from和to必须是等长的。否侧抛出异常

>>> str1 = 'abcdef'
>>> str2 = 'hijklm'
>>> string.maketrans(str1,str2)
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`hijklmghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'


replace 字符串替换。

>>> str1.replace('t','h')
'hesh'
>>> str1 = 'test'
>>> string.replace(str1, 'te','ha')
'hast'
>>> string.replace(str1, 't','h')
'hesh'
>>> str1.replace('t','h')
'hesh'

rfind 和find类似,只是返回的是最右侧位置的下标。即从左往右最后一个match的。

>>> str = 'test'
>>> str.find('es')
1
>>> str.rfind('es')
1
>>> str = 'testtest'
>>> str.find('es')
1
>>> str.rfind('es')
5

rindex 类似于index,从右侧开始   rjust 类似于rjust,右对齐输出。

>>> str1.rjust(1)
'test'
>>> str1.rjust(2)
'test'
>>> str1.rjust(3)
'test'
>>> str1.rjust(6)
'  test'
>>> string.rjust(str1, 2)
'test'
>>> string.rjust(str1, 6)
'  test'

rsplit
string.rsplit(s[, sep[, maxsplit]])
类似于split,区别为指定maxsplit时不同。

>>> str1.split(' ',2)
['This', 'is', 'a test']
>>> str1='This is a test'
>>> str1.split(' ',2)
['This', 'is', 'a test']
>>> str1.rsplit(' ',2)
['This is', 'a', 'test']
>>> str1.rsplit(' ',1)
['This is a', 'test']
>>> str1.split(' ',1)
['This', 'is a test']

rstrip 类似于rstrip,去除右侧的符号,   split 字符串切分,默认使用空白

>>> str1 = 'test'
>>> str1.split()
['test']
>>> str1.split('t')
['', 'es', '']
>>> str1.split('e')
['t', 'st']
>>> str1 = 'test one'
>>> str1.split()
['test', 'one']

splitfields 等价于split,string对象没有该方法

>>> str1 = 'this is a test'
>>> string.splitfields(str1)
['this', 'is', 'a', 'test']
>>> string.splitfields(str1,' ',2)
['this', 'is', 'a test']

strip 去除两侧的空白符

>>> str = '  this is a test  '
>>> str.strip()
'this is a test'
>>> str.lstrip()
'this is a test  '
>>> str.rstrip()
'  this is a test'

swapcase 字符串的大小写反转。

>>> str = 'This is a Test'
>>> str.swapcase()
'tHIS IS A tEST'

>>> str = 'This is 1  Test'
>>> str.swapcase()
'tHIS IS 1  tEST'

translate 使用maketrans产生的翻译表进行翻译
类似于字符串替换

>>> str1 = 'abcdefg'
>>> str2 = 'hijklmn'
>>> table = string.maketrans(str1,str2)

>>> str1.translate(table)
'hijklmn'
>>> str2.translate(table)
'hijklmn'

>>> str3 = 'opqrstu'
>>> str3.translate(table)
'opqrstu'

>>> str4 = 'hello world'
>>> str4.translate(table)
'hlllo worlk'

upper 转为大写

>>> str = 'this is a Test'
>>> str.upper()
'THIS IS A TEST'

zfill 用0在左侧填充字符串到指定长度

>>> str = 'hello world'
>>> str.zfill(10)
'hello world'
>>> str.zfill(20)
'000000000hello world'

 

refer:http://www.cnblogs.com/john2000/archive/2010/07/28/1787108.html

原创粉丝点击