栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Cheatsheet】Python基础操作

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Cheatsheet】Python基础操作

文章目录
  • 前言
  • 字符串操作
    • 找到substring的索引
  • 正则表达式(re)
    • 需要转义的字符
  • 抛出异常(Raise Exception)
  • 类(class)
    • str()和repr()
  • 小结

前言

创作开始时间:2021年11月8日21:12:36

系统性地整理一下Python经常用到的代码,该文章计划长期维护。

字符串操作 找到substring的索引
# 正向索引
str.find()
str.index() # 找不到就抛出error

# 反向索引
str.rfind()
str.rindex() # 找不到就抛出error

参考:

  • 5 Ways to Find the Index of a Substring in Python https://betterprogramming.pub/5-ways-to-find-the-index-of-a-substring-in-python-13d5293fc76d
正则表达式(re) 需要转义的字符

参考:

  • 正则表达式转义特殊字符 https://blog.csdn.net/cjx529377/article/details/78287552
抛出异常(Raise Exception)
# 展示自定义信息
raise Exception('message you want to write')

# 不展示信息也行
raise Exception

# catch方案
    try:
    	do_something()
        #raise Exception
    except Exception as e:
        print(e) # 输出exception信息
        raise # 最后抛出异常,也可以不抛出(注释这一行),继续执行后面的代码 do_other()
        #raise Exception('here is an exception')

do_other()

参考:

  • Manually raising (throwing) an exception in Python https://stackoverflow.com/questions/2052390/manually-raising-throwing-an-exception-in-python
类(class) str()和repr()

参考:

  • Python中str()与repr()函数的区别 https://www.jianshu.com/p/2a41315ca47e
小结

慢慢补充吧。

创作结束时间:2021年11月8日21:22:51

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445056.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号