Python 字符串操作
字符串
1、字符串的创建
--- 若数据类型
>>> s = 'hello python'
>>> type(s)
<class 'str'>
>>> ss = "i am zhangwanqiang"
>>> type(ss)
<class 'str'>
>>> sss = """i am a boy"""
>>> type(sss)
<class 'str'>
--- 全局函数
>>> ssss = str("this is a string")
>>> type(ssss)
<class 'str'>
2、字符串的常见方法
>>> ss
'i am zhangwanqiang'
>>> dir(ss)
['capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
--- capitalize # 首字母大写
>>> ss
'i am zhangwanqiang'
>>> ss.capitalize()
'I am zhangwanqiang'
--- center #字符串居中
>>> ss
'i am zhangwanqiang'
>>> ss.center(100, "8")
'88888888888888888888888888888888888888888i am zhangwanqiang88888888888888888888888888888888888888888'
--- strip # 取出字符串两侧的空格
>>> ss
' hello python '
>>> ss.strip()
'hello python'
--- lstrip # 字符串左对齐
>>> ss
' hello python '
>>> ss.lstrip()
'hello python '
--- rstrip # 字符串右对齐
>>> ss
' hello python '
>>> ss.rstrip()
' hello python'
--- count # 统计字符或者字符串出现的个数
>>> ss
'i am zhangwanqiang'
>>> ss.count("a")
4
--- endswith # 判断字符或者字符串是否在字符串尾部
>>> ss
'i am zhangwanqiang'
>>> ss.endswith("ang")
True
>>> ss.endswith("wan")
False
--- startswith 判断字符或者字符串是否在字符串首部
>>> ss
'i am zhangwanqiang'
>>> ss.startswith("i")
True
>>> ss.startswith("an")
False
>>> ss.startswith("am")
False
--- index、rindex # 返回字符或者字符串的索引位置(不存在抛出异常)
>>> ss
'i am zhangwanqiang'
>>> ss.index("a")
2
>>> ss.rindex("a")
15
--- find、rfind # 返回字符或者字符串的索引位置(不存在返回-1)
>>> ss
'i am zhangwanqiang'
>>> ss.find("a")
2
>>> ss.rfind("a")
15
--- encode # 将字符串转换为字节
>>> ss
'i am zhangwanqiang'
>>> tt = ss.encode("utf-8")
>>> tt
b'i am zhangwanqiang'
>>> type(tt)
<class 'bytes'>
# 字节转换为字符串
>>> tt
b'i am zhangwanqiang'
>>> type(tt)
<class 'bytes'>
>>> dd = tt.decode("utf-8")
>>> dd
'i am zhangwanqiang'
>>> type(dd)
<class 'str'>
--- format # 格式化字符串
>>> num1 = 123
>>> num2 = 321
>>> print("num = {}, num = {}".format(num1, num2))
num = 123, num = 321
--- islower # 判断字符串是否为小写字母
>>> s
'abcde'
>>> ss
'ABCDE'
>>> s.islower()
True
>>> ss.islower()
False
--- isupper # 判断字符串是否为大写字母
>>> s
'abcde'
>>> ss
'ABCDE'
>>> s.isupper()
False
>>> ss.isupper()
True
--- istitle # 判断字符串是否为标题
>>> aa
'This Is A Boy'
>>> aa.istitle()
True
--- isdigit # 判断字符串是否为数字
>>> bb = '123456'
>>> bb.isdigit()
True
--- isalpha # 判断字符串是否为字母
>>> s
'abcde'
>>> s.isalpha()
True
--- isalnum # 判断字符串是否为有效符号
>>> s
'abcde'
>>> ss
'xs23ecc3*^%'
>>> s.isalnum()
True
>>> ss.isalnum()
False
--- isspace # 判断字符串是否为空格
>>> aa
'This Is A Boy'
>>> bb
' '
>>> aa.isspace()
False
>>> bb.isspace()
True
--- title # 将字符串转换为标题
>>> aa
'i am a boy'
>>> aa.title()
'I Am A Boy'
--- 将字符串转换为大写
>>> aa
'i am a boy'
>>> aa.upper()
'I AM A BOY'
--- 将字符串转换为小写
>>> bb
'THIA IA A NUM'
>>> bb.lower()
'thia ia a num'
--- split # 将字符串切成列表
'i am a good boy'
>>> aa.split(' ')
['i', 'am', 'a', 'good', 'boy']
--- join # 将可迭代对象拼接成字符串
>>> bb
['i', 'am', 'a', 'good', 'boy']
>>> ' '.join(bb)
'i am a good boy'
--- 替换字符串中的字符串
>>> aa
'i am a good boy'
>>> aa.replace('boy', 'girl')
'i am a good girl'
3、字符串的切片
>>> ls
[112, 3, 'good', 'yesr', 242, True]
>>> ls[3:]
['yesr', 242, True]
>>> ls
[112, 3, 'good', 'yesr', 242, True]
>>> ls[1:4]
[3, 'good', 'yesr']
>>> ls
[112, 3, 'good', 'yesr', 242, True]
>>> ls[1:5:2]
[3, 'yesr']
>>> ls
[112, 3, 'good', 'yesr', 242, True]
>>> ls[::-1]
[True, 242, 'yesr', 'good', 3, 112]
>>> ls
[112, 3, 'good', 'yesr', 242, True]
>>> ls[-1:-5:-1]
[True, 242, 'yesr', 'good']
使用切片去切割数据,如果超越了下标不会报错,会返回一个空列表[]
练习:
已知一个字符串路径,如:D:\上课.视频\python\Python.脱产班\01.Python的环境安装.mp4请使用字符串和切片技术奖该路径的文件后缀名取出来。
--- 方法一
>>> ll
'D:\\上课.视频\\python\\Python.脱产班\\01.Python的环境安装.mp4'
>>> tt = ll.split()
>>> tt
['D:\\上课', '视频\\python\\Python', '脱产班\\01', 'Python的环境安装', 'mp4']
>>> tt[len(tt) -1]
'mp4'
--- 方法二
>>> ll
'D:\\上课.视频\\python\\Python.脱产班\\01.Python的环境安装.mp4'
>>> ll[ll.rfind(".") +1:]
'mp4'