Skip to content

Latest commit

 

History

History
137 lines (97 loc) · 2.71 KB

File metadata and controls

137 lines (97 loc) · 2.71 KB
Error in user YAML: (<unknown>): found a tab character that violate indentation while scanning a plain scalar at line 3 column 3
---
- oeasy Python 0478
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
- 本教程同步发布在: 
	- 个人网站: `https://oeasy.org` 
	- 蓝桥云课: `https://www.lanqiao.cn/courses/3584` 
	- GitHub: `https://github.com/overmind1980/oeasy-python-tutorial` 
	- Gitee: `https://gitee.com/overmind1980/oeasypython` 
---

时间日期

回忆

  • 上次研究了文本生成语音的方式
    • 使用tts相关功能
    • 可以把文本转化为语音文件
  • 还研究了 pypinyin 包
    • 将中文汉字转化为拼音
  • 文本str还有什么可以研究的呢?

in

"o" in "oeasy"
"u" in "oeasy"
  • in 是 序列类 中
    • 是否包含关系的 关键字
  • 字符串中
    • 可以判断 是否包含子串

图片描述

index

"oeasy".index("o")
"oeasy".index("u")
  • index 是指数
    • 根据指数 也可以判断是否包含子串

图片描述

  • index 还有其他参数

index函数细节

图片描述

  • 可以控制起止位置
"oeasy".index("o",2)
"oeasy".index("u",3)
  • 在范围里面 找不到
    • 就报ValueError

图片描述

find函数

  • find函数和index有点像

图片描述

  • 返回索引坐标

startswith

图片描述

  • 是否以某个子串开始

图片描述

endswith

  • 类似的还有endswith

图片描述

  • 是否以某个子串结束
  • 如果想判断更复杂的呢?

手机号码格式判断

  • 比如手机

图片描述

  • 以1开头 11位数字如何判断呢?
  • 写个函数

判断函数

def containsTel(s: str):
    for i in range(len(s)-10):
        if s[i] == "1":
            if s[i:i+11].isnumeric():
                return True
    return False


if __name__ == "__main__":
    s = "13901234567oeasy"
    print(s,containsTel(s))
    s = "oeasy12345678901"
    print(s,containsTel(s))
    s = "oea12345678902oeasy"
    print(s,containsTel(s))
  • 结果

图片描述

总结🤔

  • 这次了解了 字符串匹配函数
    • find
    • index
    • startswith
    • endswith
    • in
  • 对于特定模式字符串的匹配
    • 编写了一个函数
  • 可以让字符串模式匹配更简单一些吗?🤔
  • 下次再说👋🏻

  • 本文来自 oeasy Python 系统教程。
  • 想完整、扎实学 Python,
  • 搜索 oeasy 即可。