Python学习笔记(四)

目录

1.字符串的查询

2.字符串的大小写转换操作

3.字符串的内容对齐

4.字符串劈分操作

5.判断字符串

6.字符串的替换与合并

7.字符串的比较

8.字符串的切片操作

9.格式化字符串

10.字符串的编码与解码


1.字符串的查询

  • index(),查找子串substr第一次出现的位置,如果查找的子串不存在时,则抛出ValueError
  • rindex(),查找子串substr最后一次出现的位置,如果查找的子串不存在时,则抛出ValueError
  • find(),查找子串substr第一次出现的位置,如果查找的子串不存在时,则返回-1,建议使用
  • rfind(),查找子串substr最后一次出现的位置,如果查找的子串不存在时,则返回-1,建议使用
s='hello,hello'
print(s.index('lo'))          #3
print(s.find('lo'))           #3
print(s.rindex('lo'))         #9
print(s.rfind('lo'))          #9

2.字符串的大小写转换操作

  • upper():把字符串中所有字符都转成大写字母
  • lower():把字符串中所有字符都转成小写字母
  • swapcase():把字符串中所有大写字母转成小写字母,把所有小写字母都转成大写字母
  • capitalize():把第一个字符转换成大写,把其余字符转换成小写
  • title():把每个单词的第一个字符转换为大写,把每个单词的剩余字符转换为小写
s='hello,python'
a=s.upper()        #产生新的对象
print(a)           #HELLO,PYTHON
print(s)           #hello,python 原本的没有改变
s1='hello,Python'  #此处P大写
print(s1.lower())  #hello,python 产生新的对象
print(s1.swapcase())  #HELLO,pYTHON
print(s1.title())     #Hello,Python

3.字符串的内容对齐

  • center():居中对齐。第一个参数指定宽度,如果设置宽度小于实际宽度则返回原字符串;第二个参数指定填充符(可选,默认是空格)
  • ljust():左对齐。第一个参数指定宽度,如果设置宽度小于实际宽度则返回原字符串;第二个参数指定填充符(可选,默认是空格)
  • rjust():右对齐。第一个参数指定宽度,如果设置宽度小于实际宽度则返回原字符串;第二个参数指定填充符(可选,默认是空格),
  • zfill():右对齐,左边用0填充,该方法只接收一个参数,用于指定字符串的宽度,如果指定的宽度小于字符串长度,返回字符串本身
s='hello,Python'                  #字符串长度为12
print(s.center(20,'*'))           #****hello,Python****
print(s.ljust(20,'*'))            #hello,Python********
print(s.ljust(10,'*'))            #hello,Python(指定宽度小于字符串实际长度时,返回原字符串)
print(s.rjust(20))                #        hello,Python
print(s.rjust(10))                #hello,Python(指定宽度小于字符串实际长度时,返回原字符串)
print(s.rjust(20,'*'))            #********hello,Python
print(s.zfill(20))                #00000000hello,Python
print('-8910'.zfill(8))           #-0008910,0添到了减号之后

4.字符串劈分操作

  • split():从字符串的左边开始劈分,默认的劈分字符是空格,返回的值都是一个列表
  • rsplit():从字符串的右边开始劈分,默认的劈分字符是空格,返回的值都是一个列表
#可以通过参数sep指定劈分字符
#通过参数maxsplit指定劈分字符串的最大劈分次数,在经过最大次劈分之后,剩余的子串会单独做为一部分
s='hello world Python'
lst=s.split()
print(lst)                             #['hello', 'world', 'Python']
s1='hello|world|Python'
lst1=s1.split(sep='|') 
print(lst1)                            #['hello', 'world', 'Python']
print(s1.split(sep='|',maxsplit=1))    #['hello', 'world|Python']
print(s1.rsplit(sep='|',maxsplit=1))   #['hello|world', 'Python']

5.判断字符串

  • isidentifier():判断指定的字符串是不是合法字符串
  • isspace():判断指定的字符串是否全部由空白字符组成(回车、换行,水平制表符)
  • isalpha():判断指定的字符串是否全部由字母组成
  • isdecimal():判断指定字符串是否全部由十进制的数字组成
  • isnumeric():判断指定的字符串是否全部由数字组成
  • isalnum():判断指定字符串是否全部由字母和数字组成
s='hello,Python'
print('1.',s.isidentifier())         #False,合法的标识符组成是字母、数字、下划线
print('2.','hello'.isidentifier())   #True
print('3.','张三_'.isidentifier())    #True
print('4.','张三123'.isidentifier())  #True

print('5.','\t'.isspace())           #True
print('6.','abc'.isalpha())          #True
print('7.','张三'.isalpha())          #True
print('8.','张三1'.isalpha())         #False

print('9.','123'.isdecimal())        #True
print('10.','123四'.isdecimal())     #False

print('11.','123'.isnumeric())       #True
print('12.','123四'.isnumeric())     #True

print('12.','123四'.isnumeric())     #True

print('13.','abc1'.isalnum())        #True

6.字符串的替换与合并

  • replace():第一个参数指定被替换的子串,第二个参数指定替换子串的字符串,该方法返回替换后得到的字符串,替换前的字符串不发生变化,调用该方法时可以通过第3个参数指定最大替换次数
  • join():将列表或元组中的字符串合并成一个字符串
s='hello,Python'
print(s.replace('Python','world'))                #hello,world
s='hello,Python,Python,Python'
print(s.replace('Python','world',2))              #hello,world,world,Python

lst=['hello','Python','world']
print(' '.join(lst))                              #hello Python world
print('|'.join(lst))                              #hello|Python|world
print(''.join(lst))                               #helloPythonworld

t=('hello','Python','world')
print(' '.join(t))                                #hello Python world

print('*'.join('Python'))                         #P*y*t*h*o*n

7.字符串的比较

  • 运算符:>,>=,<,<=,==,!=
  • 比较规则:首先比较两个字符串中第一个字符,如果相等则继续比较下一个字符,依次比较下去,直到两个字符串中的字符不相等时,其比较结果就是两个字符串的比较结果,两个字符串中的所有后续字符串将不再被比较
  • 比较原理:两个字符进行比较时,比较的是其ordinal value(原始值),调用内置函数ord可以得到指定字符的ordinal value。与内置函数ord对于的是内置函数chr,调用内置函数chr是指定ordinal value可以得到其对应的字符
print('apple'>'app')        #True
print('apple'>'banana')     #False
print(ord('a'),ord('b'))    #97 98,原始值即ASCLL码
print(ord('桑'))            #26705

print(chr(97),chr(98))      #a b
print(chr(26705))           #桑

'''==与is的区别
==比较的是 value
is比较的是 id
'''

8.字符串的切片操作

  • 字符串是不可变类型,不具备增、删、改操作,切片操作将产生新的对象
s='hello,Python'
s1=s[:5]
s2=s[6:]
s3='!'
s4=s1+s3+s2
print(s4)

9.格式化字符串

  • %作占位符
  • {}作占位符
  • f-string
name='张三'
age=20
print('我叫%s,今年%d岁'%(name,age))            #我叫张三,今年20岁
print('我叫{0},今年{1}岁'.format(name,age))    #我叫张三,今年20岁
print(f'我叫{name},今年{age}岁')               #我叫张三,今年20岁
print('%d'%99)               #99
print('%10d'%99)             #        99,10表示占的宽度
print('%f' %3.1415926)       #3.141593
print('%.3f' %3.1415926)     #3.142
print('%10.3f' %3.1415926)   #     3.142,总宽度为10,保留小数点后三位

print('{}'.format(3.1415926))         #3.1415926
print('{:.3}'.format(3.1415926))      #3.14
print('{0:.3}'.format(3.1415926))     #3.14
print('{0:.3f}'.format(3.1415926))    #3.142
print('{0:10.3f}'.format(3.1415926))  #     3.142

10.字符串的编码与解码

  • 为什么需要进行字符串的编码转换? 答:进行传输。(爬虫的时候会用到)
  • 编码:将字符串转换为二进制数据(bytes)
  • 解码:将bytes类型的数据转换成字符串类型
s='天涯共此时'
#编码
#GBK格式中,一个中文占两个字符
print(s.encode(encoding='GBK'))         #b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
#UTF-8格式中,一个中文占三个字符
print(s.encode(encoding='UTF-8'))       #b'\xe5\xa4\xa9\xe6\xb6\xaf\xe5\x85\xb1\xe6\xad\xa4\xe6\x97\xb6'

byte=b'\xcc\xec\xd1\xc4\xb9\xb2\xb4\xcb\xca\xb1'
#解码
#byte代表一个二进制数据(字节类型的数据)
print(byte.decode(encoding='GBK'))      #天涯共此时

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

biu~!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值