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()); / ...
随机推荐
- 爬虫mm131明星照片
''' 1. 爬取以下站点中各个明星图片,分别单独建文件夹存放. 起始URL地址:http://www.mm131.com/mingxing ''' import os import logging ...
- H3C 网管交换机快速配置指南(转)
H3C交换机,5XXX,3XXX,还有部分2XXX系列都带有网管功能,可以帮助网络维护非常好的控制网络.基本的配置顺序: Console接口连接,开启Telnet登陆功能,Telnet后进行具体设置. ...
- vue中自定义软键盘
https://segmentfault.com/a/1190000012568480
- 删除 clean tomcat7:run
1.在eclipse中运行的绿色箭头旁边有个下箭头,点击: 2.选择Run Configurations... 3.在Maven Builder下删除不想要的
- Spring MVC POM示例
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- Ajax 传包含集合的JSON
通过ajax给后台传json对象,当json中含对象集合时,如 $.ajax({ url : , type : "POST", dataType : "json" ...
- js模拟链表---双向链表
双向链表: 每个元素,有一个 next(指向下一个元素)和一个prev(指向前一个元素) function dbLinkedList(){ var length=0; var head = null; ...
- MySQL--10MySQL图形化管理工具
- MySQL操作数据库--与MySQL零距离接触1-7
第一章 1-7操作数据库 数据库是一个集合:表 索引等. MySQL语句规范: 关键字与函数名称全部大写 数据库名称.表名称.字段名称全部小写 SQL语句必须以分号结尾 语法结构: {}: ...
- python 读取excel数据并将测试结果填入Excel
python 读取excel数据并将测试结果填入Excel 读取一个Excel中的一条数据用例,请求接口,然后返回结果并反填到excel中.过程中会生成请求回来的文本,当然还会生成一个xml文件.具体 ...