
Day 4字符串操作上——文字处理的十八般武艺数字运算告一段落。今天开始学字符串——编程里最频繁的操作就是处理文字。爬虫抓到的网页、读写文件、用户输入……全是字符串。一、字符串的本质字符的序列字符串其实就是一串字符按顺序排好队。Python 会给每个位置一个编号叫做索引也叫下标。字符串: H e l l o 索引: 0 1 2 3 4 反向: -5 -4 -3 -2 -1索引从0开始不是从1开始——这是编程界的通例几乎所有语言都这样。sHello# 正向索引print(s[0])# Hprint(s[1])# eprint(s[4])# o# 反向索引从-1开始倒着数print(s[-1])# o最后一个print(s[-2])# l倒数第二个print(s[-5])# H倒数第五个即第一个# 索引越界会报错# print(s[10]) # IndexError: string index out of range二、切片——一次取一段光取单个字符不够经常需要取一段。Python 的切片语法非常强大sHello World# 语法s[起始:结束:步长]# 注意包含起始位置不包含结束位置左闭右开print(s[0:5])# Hello 取索引0~4共5个字符print(s[6:11])# World 取索引6~10print(s[:5])# Hello 不写起始默认从头开始print(s[6:])# World 不写结束默认取到末尾print(s[:])# Hello World 都不写复制整个字符串左闭右开是 Python 的核心设计哲学一开始可能不习惯但用多了就会发现它的优雅# 左闭右开的好处长度计算直观# s[a:b] 的长度 b - asPythonparts[1:4]# yth 长度 4 - 1 3# 切分点从0开始无缝拼接# s[:i] s[i:] s 永远成立print(s[:3]s[3:])# Python步长第三个参数s0123456789print(s[::2])# 02468 每2个取1个偶数索引print(s[::3])# 0369 每3个取1个print(s[::-1])# 9876543210 步长为负 倒序print(s[8:2:-1])# 876543 从索引8倒着取到索引3不含索引2[::-1]反转字符串是面试常考题一行代码搞定。三、字符串常用方法上——查找与判断字符串是一个对象自带很多方法method。方法是属于某个类型的函数调用方式为字符串.方法名()。1. len()——查长度sHello Worldprint(len(s))# 11 空格也算一个字符# 实战判断用户名长度是否合法usernameadmin123iflen(username)6andlen(username)20:print(用户名长度合法)2. find() / index()——查找子串位置sHello Worldprint(s.find(o))# 4 找到第一个o的位置print(s.find(World))# 6 可以查找多个字符print(s.find(xxx))# -1 找不到返回-1不报错# index() 和 find() 功能一样但找不到会报错# print(s.index(xxx)) # ValueError 报错3. count()——统计出现次数shello hello helloprint(s.count(hello))# 3print(s.count(l))# 6print(s.count(x))# 0 没有就是04. startswith() / endswith()——判断开头结尾filenamephoto.jpgprint(filename.endswith(.jpg))# Trueprint(filename.endswith(.png))# Falseprint(filename.startswith(photo))# True# 实战判断文件类型iffilename.endswith((.jpg,.png,.gif)):print(这是一个图片文件)5. 字符类型判断print(123.isdigit())# True 全是数字print(abc.isalpha())# True 全是字母print(abc123.isalnum())# True 字母数字无特殊符号print( .isspace())# True 全是空白字符print(Hello.isupper())# False 全是大写print(HELLO.isupper())# Trueprint(hello.islower())# True 全是小写实战案例——验证密码复杂度passwordAbc12345has_digitany(c.isdigit()forcinpassword)# 包含数字has_upperany(c.isupper()forcinpassword)# 包含大写has_lowerany(c.islower()forcinpassword)# 包含小写is_long_enoughlen(password)8ifhas_digitandhas_upperandhas_lowerandis_long_enough:print(密码强度合格)else:print(密码太弱请包含大小写字母和数字至少8位)四、动手练习分析一段文本textPython is easy to learn. Python is powerful. I love Python!# 1. 统计总字符数print(f总字符数{len(text)})# 2. 统计 Python 出现了几次print(fPython出现次数{text.count(Python)})# 3. 第一个 Python 在什么位置print(f第一个Python位置{text.find(Python)})# 4. 最后一个 Python 在什么位置print(f最后一个Python位置{text.rfind(Python)})# 5. 这个句子以什么结尾print(f以!结尾吗{text.endswith(!)})# 6. 取前6个字符和后6个字符print(f前6个字符{text[:6]})print(f后6个字符{text[-6:]})五、今日学习总结学习内容掌握情况一句话要点索引✅ 理解从0开始负数从-1倒着数切片 [::]✅ 理解左闭右开[::-1]倒序len()✅ 理解统计字符个数空格也算find() / rfind()✅ 理解查找子串位置找不到返回-1startswith / endswith✅ 理解判断开头结尾文件类型判断常用isdigit / isalpha 等✅ 理解检查字符类型表单验证必备今日踩坑记录索引从0开始不是1Hello[1]是e不是H。这个坑几乎所有新手都会踩。切片[a:b]不包含 bHello[0:2]是He而不是Hel。记住公式长度 b - a。find()找不到返回-1不要写if s.find(x):来判断是否存在因为位置0找到了但在开头是False应该写if s.find(x) ! -1:或直接用if x in s:。count()区分大小写Hello.count(h)返回 0因为 H 是大写的。要忽略大小写先转成小写Hello.lower().count(h)。六、明天学什么今天学了字符串的查找和判断。明天继续字符串下篇——修改、替换、切割、格式化。特别是 f-string写 Python 天天用。字符串操作是编程的基本功。就像学书法先练笔画——每天敲几行慢慢就熟了。第4天打卡完成。明天见本系列是个人学习笔记如有错误欢迎在评论区指正交流。