如何通过91个建议提升Python代码质量?
- 内容介绍
- 文章标签
- 相关推荐
本文共计2082个文字,预计阅读时间需要9分钟。
建议36:掌握字符串的基本用法及性质判断:isalnum()、isalpha()、isdigit()、islower()、isupper()、isspace()、istitle()、startswith()、endswith()。查找和替换:count()、find()、sub()、rfind()、rindex()。
- 建议36:掌握字符串的基本用法
- 性质判断:isalnum()、isalpha()、isdigit()、islower()、isupper()、isspace()、istitle()、strartswith()、endswith()。
- 查找和替换:count()、find()、sub()、rfind()、rindex()。find()函数找不到时返回-1,index()函数则抛出ValueError()异常。对于判定是否包含子串的判断并不推荐调用这些方法,而是推荐in和not in操作符。replace()用以替换字符串的某些子串,如果指定count参数时,就最多替换count次,如果不指定就全部替换。
- 字符串的分切与连接。partition()、rpartition()、splitlines()、split()、rsplit()。
- 排版center()、ljust()、rjust()、zfill()、expandtabs()(将字符串中的制表符转换为适量的空格)。
- 建议37:按需选择sort()或者sorted()
sorted(iterable[, cmp[, key[, reverse]]])
s.sort([cmp[, key[, reverse]]])
- cmp为用户定义的任何比较函数,函数的参数为两个可比较的元素。
本文共计2082个文字,预计阅读时间需要9分钟。
建议36:掌握字符串的基本用法及性质判断:isalnum()、isalpha()、isdigit()、islower()、isupper()、isspace()、istitle()、startswith()、endswith()。查找和替换:count()、find()、sub()、rfind()、rindex()。
- 建议36:掌握字符串的基本用法
- 性质判断:isalnum()、isalpha()、isdigit()、islower()、isupper()、isspace()、istitle()、strartswith()、endswith()。
- 查找和替换:count()、find()、sub()、rfind()、rindex()。find()函数找不到时返回-1,index()函数则抛出ValueError()异常。对于判定是否包含子串的判断并不推荐调用这些方法,而是推荐in和not in操作符。replace()用以替换字符串的某些子串,如果指定count参数时,就最多替换count次,如果不指定就全部替换。
- 字符串的分切与连接。partition()、rpartition()、splitlines()、split()、rsplit()。
- 排版center()、ljust()、rjust()、zfill()、expandtabs()(将字符串中的制表符转换为适量的空格)。
- 建议37:按需选择sort()或者sorted()
sorted(iterable[, cmp[, key[, reverse]]])
s.sort([cmp[, key[, reverse]]])
- cmp为用户定义的任何比较函数,函数的参数为两个可比较的元素。

