python学习-基础-day3-字符串操作

字符串特性是不可修改

1,大小写相关的及title

1 name = 'lao cui'
2 print(name.capitalize())    # 将字符串的首字母大写
3 print(name.upper())     #小写-->大写
4 print('cUI Qhaha'.lower())     #大写-->小写
5 print(name.title())     #标题的形式,字符串中每个空格后面的首字母都大写
6 print('cUI Qhaha'.isupper())     #字符串是不是全是大写
7 print('cui qhaha '.islower())     #字符串是不是全是x小写
8 print('cUI Qhaha'.swapcase())     #大写-->小写,小写-->大写
9 print('Lao Cui Ha'.istitle())       #是不是标题的格式

输出:

1 ----
2 Lao cui
3 LAO CUI
4 cui qhaha
5 Lao Cui
6 False
7 True
8 Cui qHAHA
9 True

2,count()和列表中的用法一样

>>> test1
'lu si zhen zhu'
>>> test1.count('z')   #查找字符串中有几个z
2

3,自动补全相关

1 print('Lao cui a'.center(30,'*'))       #将字符串长度更改为30,不够的用*替代,原字符串放在中间
2 print('Lao cui a'.ljust(30,'*'))        #同上,放左边
3 print('Lao cui a'.rjust(30,'*'))        #同上
4 print('Lao cui a'.zfill(30))            #用0补全

输出:

1 **********Lao cui a***********
2 Lao cui a*********************
3 *********************Lao cui a
4 000000000000000000000Lao cui a

 4,encode(将字符串转换为bytes类型)

>>> '哈哈'.encode()
b'\xe5\x93\x88\xe5\x93\x88'

python3里默认的是utf-8

相当于

>>> '哈哈'.encode('utf-8')
b'\xe5\x93\x88\xe5\x93\x88'

5,endswith(判断字符串是否以*结尾)

>>> '****@163.com'.endswith('@163.com')
True
>>>

常用的方法:比如判断是否为有效的邮箱地址等

6,find(找到字符串中***的起始位置下标)和rfind(找到字符串最右边符合条件的第一个字符的下标)

>>> '****@163.com'.find('63.c')
6
>>>

'63.c'的第一个字符是6,下标是6


 

>>> 'abc def abc'.find('abc')
0
>>> 'abc def abc'.rfind('abc')
8
>>>

7,index(索引)跟列表一样

>>> '****@163.com'.index('6')
6
>>> '****@163.com'.index('c')
9

字符串的切片:用法跟列表一样

>>> 'woaini'[2:-1:2]
'an'
>>>

8,format(格式化)和format_map

>>> 'my name is {name},I am {year}.'.format(name = 'laocui',year = 27)
'my name is laocui,I am 27.'
>>>


>>> 'my name is {name},I am {year}.'.format_map({'name':'laocui','year':27})
'my name is laocui,I am 27.'

9,判断字符串内容

1 print('123Abc'.isalnum())   # 判断字符串是否只包含英文字符和数字
2 print('123abc'.isalpha())   #是否只包含英文字母
3 print('AbcDc'.isalpha())   #是否只包含英文字母
4 print('12.34'.isdigit())    #是否为一个整数
5 print('12345'.isdigit())    #是否为一个整数
6 print('1abcD'.isupper())  #是否都是大写
7 print('1abcd'.islower())  #是否都是小写

输出:

1 True
2 False
3 True
4 False
5 True
6 False
7 True

另外还有很多方法,不常用就不写了

>>> b="ddefdsdff_哈哈" 
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

10,join(重要)

print('+'.join(['1','2','3']))  #python3中,join里面列表中的数字都要写成字符串形式
输出:1+2+3 ,我用type输出值,返回<class 'NoneType'>

print('笨蛋'.join(['你是',',他是','。']))输出:你是笨蛋,他是笨蛋。,type,返回<class 'str'>

11,maketrans和translate

p = str.maketrans('abcdef','123456')    #'abcdef'与‘123456’一一对应
print('lao cui'.translate(p)) #替换

12,rplace(替换)

print('lao cui haha'.replace('a','A'))  #将字符串中的小a全部替换为A,默认全替换,后面数字表示从左到右替换几个
print('lao cui haha'.replace('a','A',2))

输出:

lAo cui hAhA
lAo cui hAha

13,split和splitlines(字符串转换为列表)

1 print('wo hen ai ni'.split())   #默认以空白字符(空格、Tab,换行)为分隔符
2 print('wo hen ai ni'.split('n'))  #指定以‘n’为分隔符
3 print('wo hen\n ai\n ni'.splitlines())  #以换行符为分隔符

输出:

1 ['wo', 'hen', 'ai', 'ni']
2 ['wo he', ' ai ', 'i']
3 ['wo hen', ' ai', ' ni']

split扩展:

def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
"""
S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
"""
return []

split有两个关键参数,默认是None(以空白字符(空格、Tab,换行)为分隔符),另一个是-1(默认最大分隔),,如果maxsplit给定一个小于最大分隔数值的值,如2,表示从左到右,按照分隔符最多分隔3个

如:

>>> a
'我 \nshi \n老 崔,哈 哈 哈'
>>> a.split(None,3)    ##None将任意的空白字符都作为分隔符,3表示从左到右按照条件最多分割4个
['我', 'shi', '老', '崔,哈 哈 哈']
>>>

14,strip(),lstrip(),rstrip()

1 a = '\tbc cd\tcd\n'
2 print(a)    #开头是tab,结尾是换行
3 print(a.strip())        #去掉开头和结尾的空白符,这里的tab和换行
4 print(a.lstrip())       #只去掉左边的空白符
5 print(a.rstrip())       #只去掉右边的空白符

输出:

1     bc cd    cd
2 
3 bc cd    cd
4 bc cd    cd
5 
6     bc cd    cd

加参数:

1 a = '\tbc cd\tcd\n'
2 print(a)    #开头是tab,结尾是换行
3 print(a.strip("\n"))        #只删除开头与结尾出的换行符
4 print(a.strip("\t\nbd"))       #两边的字符只要在删除的序列内,都删除
5 print(a.rstrip("\nbd"))       #右边的字符在删除的序列内,就删除,从右往左数,只有换行和d在删除的序列内

输出:

1 D:\python\python.exe "D:/script/python/study1/chapter 1/day2/1111.py"
2     bc cd    cd
3 
4     bc cd    cd
5 c cd    c
6     bc cd    c

 

 

posted @ 2017-04-30 12:28  露似真珠月似弓  阅读(228)  评论(0编辑  收藏  举报