python之字符串函数
1. endswith() startswith()
# 以什么什么结尾
# 以什么什么开始
test = "alex"
v = test.endswith('ex')
v = test.startswith('ex')
print(v)
2. expandtabs()
test = "1\t2345678\t9"
v = test.expandtabs(6)
print(v,len(v))
3. find()
# 从开始往后找,找到第一个之后,获取其未知
# > 或 >=
test = "alexalex"
# 未找到 -1
v = test.find('e')
print(v)
4. index()
# index找不到,报错 忽略
test = "alexalex"
v = test.index('a')
print(v)
5. format() format_map()
# 格式化,将一个字符串中的占位符替换为指定的值
test = 'i am {name}, age {a}'
print(test)
v = test.format(name='alex',a=19)
print(v) test = 'i am {0}, age {1}'
print(test)
v = test.format('alex',19)
print(v) # 格式化,传入的值 {"name": 'alex', "a": 19}
test = 'i am {name}, age {a}'
v1 = test.format(name='df',a=10)
print(v1)
v2 = test.format_map({"name": 'alex', "a": 19})
print(v2)
6. isalnum()
# 字符串中是否只包含 字母和数字
test = "er;"
v = test.isalnum()
print(v)
7.capitalize() 首字母大写
test = "sqy"
v = test.capitalize()
print(v)
8. casefold() lower() 所有变小写,casefold更牛逼,很多未知的对相应变小写
test = "sqy"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
9. center() 置宽度,并将内容居中
# 20 代指总长度
# * 空白未知填充,一个字符,可有可无
test = "sqy"
v = test.center(20,"中")
print(v)
10. ljust() rjust() zfill()
# test = "alex"
# v = test.ljust(20,"*")
# print(v) # test = "alex"
# v = test.rjust(20,"*")
# print(v) # test = "alex"
# v = test.zfill(20)
# print(v)
11. count() 去字符串中寻找,寻找子序列的出现次数
test = "aLexalexr"
v = test.count('ex')
print(v) test = "aLexalexr"
v = test.count('ex',5,6)
print(v)
12. isalpha() 是否是字母,汉子
test = "as大多数df"
v = test.isalpha()
print(v)
13. isdecimal() isdigit() isnumeric() 当前输入是否是数字
test = "②" # 1,②,儿
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print(v1,v2,v3)
14.isprintable() 是否存在不可显示的字符
# \t 制表符
# \n 换行
test = "gdds\tfdsfd"
v = test.isprintable()
print(v)
15. isspace() 判断是否全部是空格
test = " ass"
v = test.isspace()
print(v)
16. istitle() title() 判断是否是标题
test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)
17. join() 将字符串中的每一个元素按照指定分隔符进行拼接
test = "你是风儿我是沙"
print(test)
t = ' '
v = "_".join(test)
print(v)
18. islower() lower() isupper() upper()
test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1, v2) v1 = test.isupper()
v2 = test.upper()
print(v1,v2)
19. lstrip() rstrip() strip() 移除指定字符串 有限最多匹配
test = "\nxas12xa\n\txax1221axa "
v = test.lstrip('xa')
v = test.rstrip('9lexxexa')
v = test.strip('xa')
print(v) test.lstrip()
test.rstrip()
test.strip()
# 去除左右空白
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
print(test)
# 去除\t \n
v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)
20. maketrans() translate() 对应关系替换
v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345")
new_v = v.translate(m)
print(new_v)
21. partition() rpartition() 分割为三部分
test = "testasdsddfg"
v = test.partition('s')
print(v)
v = test.rpartition('s')
print(v)
22. split() rsplit() splitlines()
test = "sqys"
v = test.split('s',2)
print(v)
v = test.rsplit()
print(v) # 23 分割,只能根据,true,false:是否保留换行
# test = "asdfadfasdf\nasdfasdf\nadfasdf"
# v = test.splitlines(False)
# print(v)
23. swapcase() 大小写转换
test = "aLex"
v = test.swapcase()
print(v)
24. isidentifier() 字母,数字,下划线 : 标识符 def class
a = "de12_f"
v = a.isidentifier()
print(v)
25.replace() 将指定字符串替换为指定字符串
test = "alexalexalex"
v = test.replace("ex",'bbb')
print(v)
v = test.replace("ex",'bbb',2)
print(v)
26.灰魔法
一、for循环
test = "返回到合肥东方红刚才修改"
# for 变量名 in 字符串:
# 变量名
# break
# continue index = 0
while index < len(test):
v = test[index]
print(v) index += 1
print('=======') for zjw in test:
print(zjw) test = "好方法个地方个地方刚发的"
for item in test:
print(item)
break for item in test:
continue
print(item)
二、索引,下标,获取字符串中的某一个字符
test = "好方法个地方个地方刚发的"
v = test[3]
print(v)
三、切片
test = "好方法个地方个地方刚发的"
v = test[0:10] # 0=< <1
print(v)
四、获取长度
# Python3: len获取当前字符串中由几个字符组成
test = "好方法个地方个地方刚发的"
v = len(test)
print(v)
27. range() 获取连续或不连续的数字
# Python2中直接创建在内容中
# python3中只有for循环时,才一个一个创建
v = range(10)
v = range(3,10)
v = range(1,10,2)
# 帮助创建连续的数字,通过设置步长来指定不连续
v = range(0, 100, 5) for item in v:
print(item)
28.zip()
# print(list(zip(('a','n','c'),(1,2,3))))
# print(list(zip(('a','n','c'),(1,2,3,4))))
# print(list(zip(('a','n','c','d'),(1,2,3))))
#
# p={'name':'alex','age':18,'gender':'none'}
# print(list(zip(p.keys(),p.values())))
# print(list(p.keys()))
# print(list(p.values()))
#
print(list(zip(['a','b','c'],'')))
python之字符串函数的更多相关文章
- Python的字符串函数
今天用了将近一天的时间去学习Python字符串函数 上午学了17个,下午学了23个(共计40) 详细内容请见菜鸟教程--Python3字符串--Python的字符串内建函数
- python(字符串函数)
一.字符串函数 1.首字母大小写 capitalize() title() name = "xinfangshuo" print (name.capitalize()) print ...
- python笔记-字符串函数总结
字符串函数: chr() 数字转ASCII chr(96)="a" ord() ASCII转数字 ord("a")=96 isspace() 判断是否为空格 s ...
- Python学习-字符串函数操作3
字符串函数操作 isprintable():判断一个字符串中所有字符是否都是可打印字符的. 与isspace()函数很相似 如果字符串中的所有字符都是可打印的字符或字符串为空返回 True,否则返回 ...
- Python学习-字符串函数操作2
字符串函数操作 find( sub, start=None, end=None):从左到右开始查找目标子序列,找到了结束查找返回下标值,没找到返回 -1 sub:需要查找的字符串 start=None ...
- python pandas字符串函数详解(转)
pandas字符串函数详解(转)——原文连接见文章末尾 在使用pandas框架的DataFrame的过程中,如果需要处理一些字符串的特性,例如判断某列是否包含一些关键字,某列的字符长度是否小于3等等 ...
- 【python】字符串函数
1.String模块中的常量: string.digits:数字0~9 string.letters:所有字母(大小写) string.lowercase:所有小写字母 string.printabl ...
- Python之字符串函数str()
str()方法使Python将非字符串值表示为字符串: age = 23 message = "Happy" + str(age) +"rd Birthday"
- Python学习-字符串函数操作1
字符串的函数操作 capitalize():可以将字符串首字母变为大写 返回值:首字符大写后的新字符串 str = "liu" print(str.capitalize()); / ...
随机推荐
- java poi生成excel(个人例子js-jsp-java)
js代码: function exportRepQudl(){ if(confirm("导出输出页面内容?")){ var id = $("input[name='id' ...
- [ROS]一些传感器数据读取融合问题的思考
思考问题: 1. 如何实现传感器数据的融合,或者说时间同步? 比如里程计读数和雷达数据融合? void SlamGMapping::startLiveSlam() { entropy_publishe ...
- OpenResty安装(Centos7.2)
下载.解压安装包 [root]# wget https://openresty.org/download/openresty-1.11.2.5.tar.gz 安装libpq.pcre.openssl ...
- MySQL 的三个浮点类型
MySQL 支持的三个浮点类型是 FLOAT.DOUBLE 和 DECIMAL 类型. FLOAT 数值类型用于表示单精度浮点数值, DOUBLE 数值类型用于表示双精度浮点数值. 与整数一样,这些类 ...
- EF There is already an open DataReader associated with this Command
捕捉到 System.InvalidOperationException _HResult=-2146233079 _message=意外的连接状态.在使用包装提供程序时,请确保在已包装的 DbCon ...
- opencv之模糊处理
初学OpenCV的开发者很容易被OpenCV中各种滤波方法所困扰,不知道到底该用哪里一个来做滤波.表面原因看起来是因为OpenCV中各种滤波方式实在是太多太杂, 其背后原因是对各种滤波方法的应用场景认 ...
- shell基础:输入输出重定向
输出重定向将命令输出存入到文件,类似日志.便于查看.2和>>间没空格.但这种方法没用 ,命令执行时并不知道对错. /dev/null下的null就是一个垃圾箱,脚本中的一些命令并不需要保存 ...
- js判断当前页面是否有父页面,页面部分跳转解决办法,子页面跳转父页面不跳转解决 (原)
//如果当前页面存在父页面,则当前页面的父页面重新加载(即子页面父页面连带跳转) if(top.location!=self.location){ window.parent.loca ...
- windows中查看端口占用情况
说几个命令, netstat 用于查看进程端口占用情况,用法可以使用netstat -h 查看 tasklist 列出当前进程,有进程号 findstr 用于过滤字符串 大致过程就是: 1. 使用 n ...
- Unity shader学习之屏幕后期处理效果之边缘检测
边缘检测的原理是利用一些边缘检测算子对图像进行卷积操作. 转载请注明出处:http://www.cnblogs.com/jietian331/p/7232707.html 例如: 代码如下: usin ...