内置函数(分组)
数学运算函数
函数 | 原型 | 说明 |
abs() | abs(x) | 返回x的绝对值 |
pow() | pow(x,y) | 返回x的y次幂 |
round() | pound(x[,n]) | 返回浮点数x的四舍五入值,参数n指定保留的小数位数 |
divmod() | divmod(a,b) | 返回a除以b的商和余数,返回一个元组。 |
字符串处理函数
1.字符串中字符大小写的变换
函数 | 原型 | 具体说明 |
lower() | str.lower() | 将字符串str中的字母转换为小写字母 |
upper() | str.upper() | 将字符串str中的字母转换为大写字母 |
swapcase() | str.swapcase() | 将字符串str中的字母的小写转换 |
capitalize() | str.capitalize() | 将字符串str中的首字母大写 |
title() | str.title() | 将字符串str中的首字母大写,其余小写 |
2.指定输出字符串时的对齐方式
函数 | 原型 | 具体说明 |
ljust() | str.ljust(width,[fillchar]) | 左对齐输出字符串str,总宽度为参数width,不足部分以参数fillchar指定的字符填充,默认使用空格填充 |
rjust() | str.rjust(width,[fillchar]) | 右对齐输出字符串str,总宽度为参数width,不足部分以参数fillchar指定的字符填充,默认使用空格填充 |
center() | str.center(width,[fillchar]) | 居中对齐输出字符串str,总宽度为参数width,不足部分以参数fillchar指定的字符填充,默认使用空格填充 |
zfill() | str.zfill(width) | 将字符串变成width长,并且右对齐,不足部分用0补足 |
3.搜索和替换
函数 | 原型 | 具体说明 |
find() | str.find(substr,[start,[end]]) | 返回字符串str中出现的子串substr的第一个字母的位置,如果str中没有substr,则返回-1.搜索范围从start至end |
index() | str.index(substr,[start,[end]]) | 与find()函数相同,只是在str中没有substr时,index()函数会抛出一个错误 |
rfind() | str.rfind(substr,[start,[end]]) | 返回从右侧算起字符串str中出现子串substr的第一个字母的位置,如果str中没有substr,责返回-1.搜索范围从start至end |
rindex() | str.rindex(substr,[start,[end]]) | 与find()函数相同,只是在str中没有substr时,index()函数会抛出一个错误 |
count() | str.count(substr,[start,[end]]) | 计算substr在str中出现的次数.搜索范围从start至end |
replace() | str.repalce(oldstr,newstr,[count]) | 把字符串str中的oldstr替换为newstr,count为替换次数 |
strip() | str.strip([chars]) | 把字符串str中前后chars中有的字符全部去掉。如果不指定参数chars,则会去掉空白符(\n \r \t) |
lstrip() | str.lstrip([chars]) | 把字符串str中前面chars中有的字符全部去掉。如果不指定参数chars,则会去掉空白符(\n \r \t) |
rstrip() | sstr.strip([chars]) | 把字符串str中后面chars中有的字符全部去掉。如果不指定参数chars,则会去掉空白符(\n \r \t) |
expendtabs() | str.expendtabs([tabsize]) |
把字符串str中的tab字符替换为空格,每个tab替换为tabsize个空格,默认为8个 |
4.分割和组合
函数 | 原型 | 具体说明 |
split() | str.split([sep,maxsplit]) | 以sep为分割(隔)符,把str分割(隔)成一个列表。参数maxsplit表示分割(隔)的次数 |
splitlines() | str.splitlines([keepends]) | 把str按照行分割成为一个列表。参数keepends是一个布尔值,如果为True,则每行保留分割符 |
join() | str.join(seq) | 把seq代表的序列——字符串序列,用str连接起来 |
5.字符串判断相关
函数 | 原型 | 具体说明 |
startswith() | str.startswith(substr) | 判断str是否以substr开头 |
endswith() | str.endswith() | 判断str是否以substr结尾 |
isalnum() | str.isalnum() | 判断str是否全为字母或数字 |
isalpha() | str.isalpha() | 判断str是否全为字母 |
isdigit() | str.isdigit() | 判断str是否全为数字 |
islower() | islower() | 判断str是否全为小写字母 |
isupper() | isupper() | 判断str是否全为大写字母 |