一.字符串类型
str和bytes类型
>>> a = 'fdsj'
>>> type(a)
<class 'str'>
>>> type(a.encode('utf-8')) ###服务器传输的时候才会使用到bytes类型
<class 'bytes'>
二.换行和转义
三.字符串运算
下表实例变量a值为字符串"Hello",b变量值为"Python":
四.格式化
五.常用的字符串操作
1.strip函数:
该函数可以将字符串的左右两边的空格、 等空白内容或指定字符串去除,并返回处理后的结果,但原字符串并未被改变。
不带参数的strip()函数,表示把S中前后所有的空白字符全部去掉,包括’ ’ , ‘ ’ , ‘ ’ , ’ ’ 等不可见字符串,可以理解为把S前后空白
字符串替换为None;带参数的strip()函数表示将S前后指定字符串chars去掉。
用法:s.strip([chars])
实例一:
>>> s = ' boy boy boy '
>>> s
' boy boy boy '
>>> s.strip()
'boy boy boy'
实例二:
>>> s = '
boy boy boy '
>>> s
'
boy boy boy '
>>> s.strip()
'boy boy boy'
实例三:
>>> s= '*boy boy boy **'
>>> s
'*boy boy boy **'
>>> s.strip('*')
'boy boy boy '
2.lstrip()函数:可以将字符串的左边空格 等空白内容去除
实例:
>>> s= '*boy boy boy **'
>>> s
'*boy boy boy **'
>>> s.lstrip('*')
'boy boy boy **'
rstrip()函数:可以将字符串的左边空格 等空白内容去除
实例:
>>> s= '*boy boy boy **'
>>> s
'*boy boy boy **'
>>> s.rstrip('*')
'*boy boy boy
3.大小写互换
S.lower()函数:将字符串转换为小写
>>> 'S'.lower()
's'
S.upper()函数:将字符串转化为大写
>>> 'boY'.upper()
'BOY'
S.swapcase()函数:将字符串的大小写互换
>>> 'I am'.swapcase()
'i AM'
S.capitalize()函数:将字符串的首个字母转换成大写
>>> 'abc'.capitalize()
'Abc'
S.title() 函数:将字符串的每个单词首字母大写 ##跟string模块的capwords()函数功能类似
>>> 'i am a man'.title()
'I Am A Man'
>>> import string
>>> s = 'i am a man'
>>> string.capwords(s)
'I Am A Man'
练习:
将所有的字符串内容中的字母均替换为大写
def upper(s): if not isinstance(s,str): return s result = '' for i in s: if (i >='a' and i<= 'z'): result += chr(ord(i)-32) else: result += i return result print(upper('我么abcABC&^#')) 结果为:我么ABCABC&^#
将所有的字符串内容中的字母均替换为小写
def lower(s): if not isinstance(s,str): return s result = '' for i in s: if (i >='A' and i<= 'Z'): result += chr(ord(i)+32) else: result += i return result print(lower('我么SNLKJDabcABC')) 结果:我么snlkjdabcabc
4.字符串对齐
S.ljust()函数:S.ljust(width,[fillchar])#输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空
>>> '123'.ljust(10,'*')
'123*******'
###默认不写第二个参数,则使用空格填充
>>> '123'.ljust(10)
'123 '
###如果没有空余的位置,则没有填充字符
>>> '123'.ljust(3,"*")
'123'
S.rjust()函数:S.rjust(width,[fillchar]) #右对齐
>>> '123'.rjust(10,'*')
'*******123'
S.center()函数:S.center(width, [fillchar]) #中间对齐
>>> '123'.center(9,'&')
'&&&123&&&'
##先后面在前面
>>> '123'.center(10,'&')
'&&&123&&&&'
S.zifll()填充:S.zfill(width) #把S变成width长,并在右对齐,不足部分用0补足
>>> 'abc'.zfill(6)
'000abc'
5.字符串搜索
s.find()函数:可在指定字符串范围内查找子字符串出现的位S.find(substr, [start, [end]])#返回S中出现substr的第一个字母的标号
>>> 'i am a man'.find('a')
2
>>> 'i am a man'.find('a',3)
5
start和end作用就相当于在S[start:end]中
>>> 'i am a man'.find('a',5,7)
5
如果S中没-1
>>> 'i am a man'.find('a',6,7)
-1
s.index()函数:可在指定字符串范围内查找子字符串出现的位置,找不到则返回错S.index(substr, [start, [end]])#与find()相同,只是在S中没有substr时,会返回一个运行时错误
>>> 'i am a boy'.index('a')
2
>>> 'i am a boy'.index('a',3)
5
>>> 'i am a boy'.index('a',6,7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
s.rfind()函数:可在指定字符串范围内查找子字符串出现的位置S.rfind(substr, [start, [end]])
#返回S中最后出现的substr的第一个字母的标号,如果S中没有substr则返回-1,也就是说从右边算
起的第一次出现的substr的首字母标号
>>> 'i am a boy'.rfind('a')
5
>>> 'i am a boy'.rfind('a',3)
5
>>> 'i am a boy'.rfind('a',1,3)
2
>>> 'i am a boy'.rfind('a',6,7)
-1
S.rindex()函数:可在右侧指定字符串范围内查找子字符串出现的位置,找不到则报错。
S.rindex(substr, [start, [end]])#找不到则会报错误
>>> 'i am a boy'.rindex('a')
5
>>> 'i am a boy'.rindex('a',1,3)
2
>>> 'i am a boy'.rindex('a',1,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
练习: