常用python内置函数
2017-07-25 11:07
253 查看
1.数字运算函数
2.字符串处理函数
①大小写变换
②指定输出字符串的对齐方式
③搜素和替换
④分割和组合
例子
结果:
[‘hello’,’world’,’python’]
[‘hello world’,’python’]
hello#world#python
⑤字符串判读相关
函数 | 原型 | 具体说明 |
---|---|---|
abs() | abs(x) | 返回x的绝对值 |
pow() | pow(x,y) | 返回x的y次幂 |
round() | round(x[,n]) | 返回浮点数x的四舍五入值,参数n指定保留的小数位数 |
divmod() | divmod(a,b) | 返回a除以b的商和余数,返回一个元组,例如:divmod(a,b)返回(a/b,a%b) |
①大小写变换
函数 | 原型 | 具体说明 |
---|---|---|
lower() | str.lower() | 将字符串中str中的大写字母转换为小写字母 |
upper() | str.upper() | 将字符串中str中的小写字母转换为大写字母 |
swapcase() | str.swapcase() | 将字符串str中的字母大小写互换 |
capitalize() | str.capitalize() | 将字符串str的首字母大写 |
title() | str.title() | 将字符串中的首字母大写,其余为小写 |
函数 | 原型 | 具体说明 |
---|---|---|
ljust() | str.ljust(width,[fillchar]) | 左对齐输出字符串str,总宽度为width,不足部分以参数fillchar指定的字符填充,默认用空格填充 |
rjust() | str.rjust(width,[fillchar]) | 同上 |
center() | str.center(width,[fillchar]) | 居中对齐 |
zfill() | str.zfill(width) | 将字符串变成width长,并且右对齐,不足之处用0补齐 |
函数 | 原型 | 具体说明 |
---|---|---|
find() | str.find(substr,[start,[end]]) | 返回字符串str中出现子串substr的第一个字母的位置,如果没有,返回-1,搜索范围从start至end |
index() | str.index(substr,[start,[end]]) | 与find()函数相同,只是在str中没有substr时,返回一个运行错误 |
rfind() | str.rfind(substr,[start,[end]]) | 返回从右侧算起str中出现子串substr的第一个字母的位置,如果没有,返回-1,搜索范围从start至end |
rindex() | str.rindex(substr,[start,[end]]) | 同index(),从右侧算 |
count() | str.count(substr,[start,[end]]) | 计算substr在str中出现的次数 |
replace() | str.replace(oldstr,newstr,[count]) | 把str中的oldstr替换为newstr,count为替换次数 |
strip() | str.strip([chars]) | 把字符串str中前后chars中有的字符全部去掉。如果不指定参数chars,则会去掉看空白字符(包括’\n’,’\r,’\t’,”) |
lstrip() | 同上 | 字符串前面,其余同上 |
rstrip() | 同上 | 字符串后面,其余同上 |
expandtabs() | str.expandtabs([tabsize]) | 把字符串str中的tab字符替换为空格,每个tab替换为tabsize个空格,默认是8个 |
函数 | 原型 | 具体说明 |
---|---|---|
split() | str.split([sep,[maxsplit]]) | 以sep为分隔符,把str分割成一个列表。参数maxsplit表示分割的次数 |
splitlines() | str.splitlines([keepends]) | 把str按照行分割符分为一个列表。参数keepends是一个布尔值,如果为True,则每行后面会保留分割符 |
join() | str.join(seq) | 把seq代表的序列—–字符串序列,用str连接起来 |
str1="hello world python" list1=str1.split(" ") print list1 str1="hello world\npython" print list1 list1=["hello","world","python"] str1="#" print(str1.join(list1))
结果:
[‘hello’,’world’,’python’]
[‘hello world’,’python’]
hello#world#python
⑤字符串判读相关
函数 | 原型 | 具体说明 |
---|---|---|
startswith() | str.startswith(substr) | 判断str是否以substr开头 |
endswith() | str.endsith(substr) | 判断str是否以substr结尾 |
isalnum() | str.isalnum() | 判断str是否全为字母或数字 |
isalpha() | str.isalpha() | 判断str是否全为字母 |
isdigit() | str.isdigit() | 判断str是否全为数字 |
islower() | str.islower() | 判断str是否全为小写字母 |
isupper() | str.isupper() | 判断str是否全为大写字母 |
相关文章推荐
- Python常用内置函数总结
- python基础知识整理——常用内置函数整理
- python 常用内置函数
- python中常用的内置函数filter,setattr,delattr,getattr,hasattr
- Python常用内置函数
- python常用内置函数format()
- Python常用内置函数
- 【Python】从简单案列中揭示常用内置函数以及数据类型
- Python-老男孩-01_基础_文件IO_函数_yield_三元_常用内置函数_反射_random_md5_序列化_正则表达式_time
- Python常用内置函数(二)
- python常用内置函数
- Python常用内置函数介绍【filter,map,reduce,apply,zip】
- python-常用内置函数之type()
- Python常用的几种常用的内置函数
- Python常用内置函数(一)
- Python常用内置函数总结
- Python常用内置函数
- Python3常用内置函数
- Python字符串内置函数(常用)
- Python常用内置函数整理(lambda,reduce,zip,filter,map)