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()); / ...
随机推荐
- Nancy Web框架 文档
http://liulixiang1988.github.io/nancy-webkuang-jia.html 中文 https://github.com/NancyFx/Nancy/wiki/Doc ...
- 小程序 login
app.json : 配置文件 => 文件路径 pages .配置窗口 window.底部导航 tabBar .请求超时时间 networkTimeout app.js : 请求路口文件 wx. ...
- git add 所有文件
git add 多个文件的格式介绍 git提交修改之前要add然后commit,假如我用git clone远程服务器文件到本地,然后我如果修改了其中几个文件要提交,我必须一个个的如: $git add ...
- Linux简单版重要安装步骤
1.稍后安装操作系统 2.自定义硬件 3.选择镜像(mini) 4.开机选择第一个 5.skip(跳过检测) 6.是,忽略所有数据 7.配置网络 8.设置root用户密码 9.使用所有空间 10.写入 ...
- mysql相关SQL
1.mysql分组获取最新数据 sql> select max(column_name) from table group by column_name having count(*) orde ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
- mysql数据库字段类型详解
MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型.本节首先给出可用类型的一个概述,并且总结每个列类型的存储需求,然后提供每个类中的类型性质的更详细的描述. ...
- 兼容PC和手机触摸代码
测试环境:unity3d 4.6.1 using UnityEngine; using System.Collections; public class neccc : MonoBehaviour { ...
- Oracle推进SCN系列:使用oradebug在mount状态下推进SCN
环境:RHEL 6.5(x86-64) + Oracle 11.2.0.4 声明:推进SCN属于非常规恢复范畴,不建议非专业人员操作,否则后果自负. 需求:我这里演示下推进SCN 10W数量级,实际需 ...
- sift拟合详解
1999年由David Lowe首先发表于计算机视觉国际会议(International Conference on Computer Vision,ICCV),2004年再次经David Lowe整 ...