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()); / ...
随机推荐
- 关于Redis的配置
Redis-配置 1. Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 daemonize no 2. 当Redis以守护进程方式运行时,Redis默认会把pid ...
- python基础(9)-迭代器&生成器函数&生成器进阶&推导式
迭代器 可迭代协议和迭代器协议 可迭代协议 只要含有__iter__方法的对象都是可迭代的 迭代器协议 内部含有__next__和__iter__方法的就是迭代器 关系 1.可以被for循环的都是可迭 ...
- ArcGIS工具备忘
1.Repair Geometry (Data Management) 几何图形修复,比如面图层不满足节点坐标逆时针 2.Raster Domain (3D Analyst) 获取栅格范围 3.Int ...
- PHP递归方法实现前序、中序、后序遍历二叉树
二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...
- cloud-api-service和cloud-iopm-web提交merge方法
cloud-api-service应该push to '自己的分支',然后去gitserver请求合并 cloud-iopm-web(master分支)应该push to Upstream,然后去gi ...
- [LeetCode] 系统刷题5_Dynamic Programming
Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题 ...
- 虚拟IP技术
虚拟IP技术在高可用领域像数据库SQLSERVER.web服务器等场景下使用很多,很疑惑它是怎么实现的,偶然,发现了一种方式可以实现虚拟ip.它的原理在于同一个物理网卡,是可以拥有多个ip地址的,至于 ...
- 数据分析与挖掘 - R语言:贝叶斯分类算法(案例三)
案例三比较简单,不需要自己写公式算法,使用了R自带的naiveBayes函数. 代码如下: > library(e1071)> classifier<-naiveBayes(iris ...
- iOS 测试企业应用的分发
开发者能够方便地来做iOS应用的测试分发,目前可以选用“浦公英”和“Fir.im”来做. 生成IPA文件 生成应用的 IPA 文件可以使用命令行 xcodebuild exportArchive -e ...
- Appium基础(四)查找app的appActivity与appPackage
要查看appActivity需要借助日志:adb logcat>D:/log.log 前提是已经装了Android SDK 在目录D:\Program Files (x86)\android\ ...