python笔记(九):字符串str查询、大小写传唤、拆分、替换、切割
-----学习视频来源于马士兵教育-----
内容主要为了自己复习用,也可以让大家梳洗思路
###字符串学习 #一、强制两个字符串ID相同 a=('a%') b=('a%') a=sys.intern(b) print(a is b)#二、字符串的查询操作 index不存在报错,find不存在返回-1 s='hello,worldlo' print(s.index('lo')) #3 第一次出现lo的位置 print(s.find('lo')) #3 第一次出现lo的位置 print(s.rindex('lo')) #11 最后一次出现lo的位置 print(s.rfind('lo')) #11 最后一次出现lo的位置#三、字符串大小写转换 s='hello.world' s1=s.upper() print(s1) #HELLO.WORLD upper,所有字符转换成大写s2=s1.lower() print(s2) #hello.world lower,所有字符变成小写s='Hello.World' s3=s.swapcase() print(s3) #hELLO.wORLD swapcase把所有大写字符转换成小写,小写字母转换成大写s='hello.World' s4=s.capitalize() print(s4) #Hello.world capitalize把第一个字符转换成大写,把其余字符都转换成小写s='hello.world' s5=s.title() print(s5) #Hello.World title把每个单词的第一个字符转换成大写,剩余字符变为小写##字符串内容对齐操作的方法 s='hello,Python' ##共12个字符,设定为20,剩余间隔为20-12,默认间隔符为空格 print(s.center(20,"*")) #****hello,Python****居中对其 print(s.ljust(20,"*")) #hello,Python******** 左对齐 print(s.rjust(20,"*")) #********hello,Python 右对齐 print(s.zfill(20)) #00000000hello,Python, 0为填充间隔,右对齐 print('-99'.zfill(4)) #-099 加上符号换算位置##字符串分割 s='hello world' s1=s.split() print(s1) #['hello', 'world'] s2='hello|wo/rl/d' print(s2.split(sep='/')) ##['hello|wo', 'rl', 'd'] 指定/为分隔符 print(s2.split(sep='/',maxsplit=1)) #['hello|wo', 'rl/d'] print(s.rsplit()) #['hello', 'world'] 默认以空格为分隔符 print(s2.rsplit('/')) #['hello|wo', 'rl', 'd'] print(s2.rsplit(sep='/',maxsplit=1)) #['hello|wo/rl', 'd'] 从右边第一个进行分割###字符串判断 s='hello,world' print(s.isidentifier()) #False 判断是不是合法标识符,字母汉字 print('s'.isidentifier()) #True print('涨弹1'.isidentifier()) #True print('\t'.isspace()) #True 是否是制表符 print('abc'.isalpha()) #True 是否是字母组合 print('123'.isnumeric()) #True 是否是纯数字组合 print('123'.isdecimal()) #True 是否是数字组合,包含罗马数字 print('asb123'.isalnum()) #True 是否是数字+字母组合##字符串替换 s='hello,world,world' print(s.replace('world','java',1)) #hello,java,world 把第二个位置的world改为javalis=['hello','world','python'] print('|'.join(lis)) #hello|world|python 数组连接,|为分隔符lis1=('hello','world') print('|'.join(lis1)) #hello|world 元组连接print('*'.join('python')) ##p*y*t*h*o*n#字符串比较 print('apple'>'app') #True print(ord('a')) ##97 比较ord值 print(chr(97)) ##a#字符串切割从左往右是从0开始,从右往左是从-1开始 s='hello,world' s1=s[:5] s2=s[6:] s3='!' news=s1+s3+s2 print(s1) #hello print(s2) #world print(news) ##hello!worldprint(s[1:5:2]) ##el, 从1开始到5,步长为2 print(s[::2]) #hlowrd 每隔两个打印输出 print(s[::-1]) #dlrow,olleh 倒排 print(s[-5::1]) #world 从右开遍第五个往前输出 #格式化字符串 #%d%i 数字 #%s 字符 #%f 浮点数 name='宋' age=18 print('我叫%s,年龄%d' % (name,age)) #我叫宋,年龄18 print('我叫{0},年龄{1}'.format(name,age)) #我叫宋,年龄18 print(f'我叫{name},年龄{age}') #我叫宋,年龄18#结果省略 print('%10d' % 99) # 99 print('%.3f' % 3.1415926) #3.142 print('{0}'.format(3.1415926)) ##3.142 print('{0:.3f}'.format(3.1415926)) ##3.142#字符串编码解码 s='凤凰花开的路口' print(s.encode(encoding='GBK')) #b'\xb7\xef\xbb\xcb\xbb\xa8\xbf\xaa\xb5\xc4\xc2\xb7\xbf\xda' print(s.encode(encoding='utf-8')) #b'\xe5\x87\xa4\xe5\x87\xb0\xe8\x8a\xb1\xe5\xbc\x80\xe7\x9a\x84\xe8\xb7\xaf\xe5\x8f\xa3' jie=s.encode(encoding='GBK') print(jie.decode(encoding='GBK')) #凤凰花开的路口 jie=s.encode(encoding='utf-8') print(jie.decode(encoding='utf-8')) #凤凰花开的路口
python笔记(九):字符串str查询、大小写传唤、拆分、替换、切割相关推荐
- 【Python笔记】字符串常见操作
Python字符串常见操作 字符串的拼接 计算字符串的长度 截取字符串 分隔字符串 字符串的替换 字符串的检索 字符串大小写转换 去掉字符串中的空格和特殊字符 格式化字符串 字符串的拼接 字符串不允许 ...
- python笔记5 - 字符串格式化表达式,while语句嵌套,for循环,break,continue,死循环
2017/9/29 字符串格式化表达式,while语句嵌套,for循环,break,continue,死循环 ============================================= ...
- 【python笔记九】字典创建、字典增删改查、字典常用操作
笔记九 python字典 字典创建 1.直接使用{} 2.使用dict() 3.空字典 字典的增删改查 增加 删除 修改 查找 字典的操作方法 get() keys() items() values( ...
- 笨方法“学习python笔记之字符串
字符串(str)是python的一种重要的数据格式,官方文档中对字符串的解释为一种文本序列格式,其内容不可修改(有点类似元组),官方解释如下: 字符串表示 可以看到官方文档里面,给出了字符串的三种表示 ...
- 字符串从右截取_跟运维组学Python基础day04(字符串str的索引和切片)
内容回顾 跟运维组学Python基础 day03 格式化输出 %s name = input('Pleases input your name: ') # Zanaoprint('My name is ...
- python笔记之序列(str的基本使用和常用操作)
序列 序列:列表(list),元组(tuple),字典(dict),集合(set),str,range() 有序列表: list,tuple 无序列表: dict,set 可变序列: list,dic ...
- 初学者python笔记(字符串用法解析)
文章目录 1. 布尔值方面 2. int()方法后面可以带参数 3. bit_lenth()方法 4. 字符串变换大小写.字符串占格 5. 字符串的去除空格.某个字符 6. 数字字符在字符串中出现的次 ...
- [转载] 初学者python笔记(字符串用法解析)
参考链接: Python中的casefold()字符串 文章目录 1. 布尔值方面2. int()方法后面可以带参数3. bit_lenth()方法4. 字符串变换大小写.字符串占格5. 字符串的去除 ...
- python笔记1:字符串处理函数
常用的字符串处理函数 1. re.S 对于re.findall(),第三个参数默认为空 返回值为一个列表 import re data = re.findall(r'(.*?)',text,re.S) ...
最新文章
- linux c 符号表,C中的符号表
- STM32上使用JSON
- pythonmatplotlib刷新_matplotlib:如何刷新figure.canvas
- 可以改位置吗_恒大文化旅游城售楼部位置外地人可以买吗优选好房泗洪
- 1.JUC锁的一些概念
- Elasticsearch查询速度为什么这么快?
- NLP学习—17.基于BM25、tfidf和SIF的检索系统实现
- 开启关闭Centos的自动更新
- 【Bug解决】yum提示Another app is currently holding the yum lock; waiting for it to exit...
- oracle用exp定时备份数据库,oracle exp备份数据库
- webstrom无法格式化局部html,格式化代码失效webstorm
- 屁孩君儿子讲解 2022 【例4.7】最小n值
- Can‘t locate XXX/XXX.pm in @INC (you may need to install the XXX::XXX module)
- Java基础每日一练—第5天:预测身高案列
- 【Linux命令】su 和 sudo
- 权威公布岑巩马家寨为一代佳人陈圆圆归隐地
- Flowable入门系列文章90 - 一般可流动的REST原则 01
- Java判定一个数值是否在指定的开闭区间范围内
- 物联网智慧农业的方案介绍(一)【方案篇01】
- SQL 函数总结说明
热门文章
- 【1.3】Channel
- 帝国cmssitemap.php,帝国CMS如何自动生成sitemap.xml网站地图
- linux raid卡缓存,RAID卡的缓存与磁盘自带的缓存的关系
- Web Apps来袭,html5解放开发者
- 三角形网格 四方形网格_HTML5中3D对象的三角形网格
- php decimal类型,decimal是什么类型
- 24.模拟多时钟定时器
- 法蒂玛机器人_机器佣人法蒂玛
- flex布局实现整体居中最后一行靠左
- 无规矩不成方圆,代码规范到底有多重要?(附字节跳动必问的HashMap源码总结)