python成长之路3——字符串操作

>>> name = "alex"                                                                                                                                                                                                   
>>> name.capitalize()   首字母大写
'Alex'
 
>>> name.center(20)     居中(字符总长,填充字符)
'        alex        '
>>> name.center(20,"$")
'$$$$$$$$alex$$$$$$$$'
>>> s = 'hello alex'           
>>> s.ljust(20)                 左对齐(右填充)
'hello alex          '
>>> s.ljust(20,'+')                20是总长度 
'hello alex++++++++++'
>>> s.rjust(20,'+')                右对齐(左填充)
'++++++++++hello alex'
>>> name = "alex"       切片
>>> name[1:4]
'lex'
 
>>> name = "dhosacnwugxasd"    
>>> name.count("s")          统计字符出现次数,可指定范围
2
>>> name.count("s",0,5)
1
 
>>> name.startswith('d')           是否以它开头
True
>>> name.endswith("x")         是否以它开头
False
>>> name.endswith("d")
True
>>> name.endswith("sd")
True
 
>>> name = "ale x"  
>>> name
'ale\tx'
>>> name.expandtabs()  将tab键转换成空格,默认转换成4个空格
'ale     x'
>>> name.expandtabs(5)
'ale  x'
 
>>> name = "alexsdfx"  查找元素的索引,可指定范围
>>> name.find("x")  第一个x
3
>>> name.find("q")
-1
>>> name.find("lex")   l的位置
1
>>> name.find("x",4,)
7
>>> s
'hello alex'
>>> s.rfind('e')       从右边开始找的第一个
8
 
>>> name.index('x')           与find唯一区别是如果元素不存在则返回报错信息
3
>>> name.index('q')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> s
'hello alex'
>>> s.rindex('e')             返回右边第一个索引
8
 
>>> name = "i m {0},age {1}"      字符串格式化,{0}{1}{ss}{dd}都是占位符
>>> name.format('zy',78)              {0}{1}这种数字占位符要按顺序传入参数
'i m zy,age 78'
>>> name = "i m {ss},age {dd}"        {ss}{dd}可理解为变量占位符
>>> name.format(dd=78,ss="alex")
'i m alex,age 78'
 
>>> name = "i m {0},age {1}"    
>>> li=["alex",78]                              可将列表和元组作为参数传入,注意要加*
>>> name.format(*li)
'i m alex,age 78'
>>> tul=("alex",78,)
>>> name.format(*tul)
'i m alex,age 78'
 
>>> name = "i m {ss},age {dd}"      也可将字典作为参数传入,注意要加**
>>> dic={"ss":"alex","dd":78}
>>> name.format(**dic)
'i m alex,age 78'
 
>>> name
'alexsdfx'
>>> name.isalnum()         字母和数字返回True,否则返回False
True
>>> name = "$%^&"
>>> name.isalnum()
False
 
>>> name = '123'  
>>> name.isalpha()          是不是字母
False
>>> name.isdigit()           是不是数字
True
>>> name = 'alex'
>>> name.islower()          所有字母是不是小写
True
>>> name = 'Alex' 
>>> name.islower()
False
>>> name = 'aLex' 
>>> name.islower()
False
>>> name.isupper()       所有字母是不是大写
False
>>> s = 'Alex'
>>> s.lower()               全变小写
'alex'
>>> s.upper()                全变大写
'ALEX'
>>> s.swapcase()          大写变小写,小写变大写
'aLEX'
>>> name.isspace()        是不是空格
False
>>> name.istitle()           是不是标题
False
>>> name = "alex is a teacher"
>>> s1 =  name.title()      将字符串转换成标题
>>> s1
'Alex Is A Teacher'             所有首字母都是大写
>>> s1.istitle()
True
>>> li
['alex', 'is', 'a', 'teacher']
>>> ' '.join(li)                  合并字符串
'alex is a teacher'
>>> l1 = ' '.join(li)
>>> l1
'alex is a teacher'
>>> l2 = '_'.join(li)
>>> l2
'alex_is_a_teacher'
>>> l3 = l1.split()          分割字符串,默认以空格分割,得到的是list
>>> l3
['alex', 'is', 'a', 'teacher']       
>>> l4 = l2.split('_')
>>> l4
['alex', 'is', 'a', 'teacher']
>>> l5 = l2.split('_',2)           可指定分割次数
>>> l5
['alex', 'is', 'a_teacher']
 
>>> s
'hello alex'
>>> s='hello alex welcome'
>>> s.split(' ',1)
['hello', 'alex welcome']
>>> s.rsplit(' ',1)             从右边开始分割
['hello alex', 'welcome']
 
>>> dic = {'aa':'11','bb':'22','cc':'33'}
>>> '_'.join(dic.values())               
'11_33_22'
>>> '_'.join(dic.keys())                 
'aa_cc_bb'
>>> '_'.join(dic)
'aa_cc_bb'
 
>>> s
'hello alex'
>>> s
'hello alex'
>>> s.partition('l')           对字符串进行分割  得到的是元组
('he', 'l', 'lo alex')
>>> s.rpartition('l')          从右边
('hello a', 'l', 'ex')
 
>>> s = '   sdf  '
>>> s.strip()           空白移除
'sdf'
>>> s = '   s     d      f           '  移除的是两边的空格以及换行符之类的,注意字母之间的空格不移除
>>> s.strip()
's     d      f'
>>> s.lstrip()           左边空白移除
'sdf  '
>>> s.rstrip()           右边空白移除
'   sdf'
 
>>> s
'hello alex'
>>> s.replace('al','AL')   替换字符,所有匹配都会替换
'hello ALex'
 
 
>>> name
'dhosacnwugxasd'
>>> name.__contains__('sd')   是否包含字符
True
>>> name.__contains__('sq')
False
 
>>> name = 'alex'
>>> name*5                  直接乘得到5个alex
'alexalexalexalexalex'
 
__   __在面向对象的时候再关注
 
posted @ 2017-02-15 17:04  meitangyanyan  阅读(125)  评论(0编辑  收藏  举报