- 生成字符串变量str='python String function'
- 字符串长度获取:len(str)
例:
>>> print '%s length=%d' % (str,len(str))
python String function length=22 - 字母处理
全部大写:str.upper()
全部小写:str.lower()
大小写互换:str.swapcase()
首字母大写,其余小写:str.capitalize()
首字母大写:str.title()
>>> print '%s lower=%s' % (str,str.lower())
python String function lower=python string function
>>> print '%s upper=%s' % (str,str.upper())
python String function upper=PYTHON STRING FUNCTION
>>> print '%s swapcase=%s' % (str,str.swapcase())
python String function swapcase=PYTHON sTRING FUNCTION
>>> print '%s capitalize=%s' % (str,str.capitalize())
python String function capitalize=Python string function
>>> print '%s title=%s' % (str,str.title())
python String function title=Python String Function
>>> -
格式化相关
获取固定长度,右对齐,左边不够用空格补齐:str.ljust(width)
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.ljust(width)
获取固定长度,右对齐,左边不足用0补齐
>>> print '%s ljust=%s' % (str,str.ljust(20))
python String function ljust=python String function
>>> print '%s rjust=%s' % (str,str.rjust(20))
python String function rjust=python String function
>>> print '%s center=%s' % (str,str.center(20))
python String function center=python String function
>>> print '%s zfill=%s' % (str,str.zfill(20))
python String function zfill=python String function
>>> -
字符串搜索相关
搜索指定字符串,没有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及结束位置搜索:str.find('t',start,end)
从右边开始查找:str.rfind('t')
搜索到多少个指定字符串:str.count('t')
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
>>> print '%s find nono=%d' % (str,str.find('nono'))
python String function find nono=-1
>>> print '%s find t=%d' % (str,str.find('t'))
python String function find t=2
>>> print '%s find t from %d=%d' % (str,1,str.find('t',1))
python String function find t from 1=2
>>> print '%s find t from %d to %d=%d' % (str,1,2,str.find('t',1,2))
python String function find t from 1 to 2=-1
>>> print '%s index nono ' % (str,str.index('nono',1,2))
Traceback (most recent call last):
File " ", line 1, in
print '%s index nono ' % (str,str.index('nono',1,2))
ValueError: substring not found
>>> print '%s rfind t=%d' % (str,str.rfind('t'))
python String function rfind t=18
>>> print '%s count t=%d' % (str,str.count('t'))
python String function count t=3
>>> -
字符串替换相关
替换old为new:str.replace('old','new')
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
>>> print '%s replace t to *=%s' % (str,str.replace('t', '*'))
python String function replace t to *=py*hon S*ring func*ion
>>> print '%s replace t to *=%s' % (str,str.replace('t', '*',1))
python String function replace t to *=py*hon String function
>>> -
字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
>>> print '%s strip=%s' % (str,str.strip())
python String function strip=python String function
>>> print '%s strip=%s' % (str,str.strip('p'))
python String function strip=ython String function
>>> print '%s strip=%s' % (str,str.rstrip('n'))
python String function strip=python String functio
>>> -
按指定字符分割字符串为数组:str.split(' '),默认按空格分隔
>>> str
'python String function'
>>> print '%s strip=%s' % (str,str.split())
python String function strip=['python', 'String', 'function']
>>> str='python-String-function'
>>> print '%s strip=%s' % (str,str.split('-'))
python-String-function strip=['python', 'String', 'function']
>>> -
字符串判断相关
是否以start开头:str.startswith('start')
是否以end结尾:str.endswith('end')
是否全为字母或数字:str.isalnum()
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
>>> str='python String function'
>>> print '%s startwith t=%s' % (str,str.startswith('t'))
python String function startwith t=False
>>> print '%s endwith d=%s' % (str,str.endswith('d'))
python String function endwith d=False
>>> print '%s isalnum=%s' % (str,str.isalnum())
python String function isalnum=False
>>> str='pythonStringfunction'
>>> print '%s isalnum=%s' % (str,str.isalnum())
pythonStringfunction isalnum=True
>>> print '%s isalpha=%s' % (str,str.isalpha())
pythonStringfunction isalpha=True
>>> print '%s isupper=%s' % (str,str.isupper())
pythonStringfunction isupper=False
>>> print '%s islower=%s' % (str,str.islower())
pythonStringfunction islower=False
>>> print '%s isdigit=%s' % (str,str.isdigit())
pythonStringfunction isdigit=False
>>> str='3423'
>>> print '%s isdigit=%s' % (str,str.isdigit())
3423 isdigit=True
>>>
【原创】Python内置的字符串处理函…
最新推荐文章于 2023-04-03 16:59:08 发布
常用的Python 内置的各种字符串处理 函数的使用方法