python字符串常用方法介绍,基于python3.10
python字符串常用方法-目录:
1、strip()、lstrip()、rstrip()
2、removeprefix()、removesuffix()
3、replace()
4、split()、rsplit()
5、join()
6、upper()、lower()、capitalize()
7、islower()、isupper()、isalpha()、isnumeric()、isalnum()
8、count()
9、find()、rfind()
10、startswith()、endswith()
11、partition()
12、center()、ljust()、rjust()
13、字符串专用f表达式
14、swapcase()
15、zfill()
- - - - - - - - - - 分割线- - - - - - - - - -
1、strip()、lstrip()、rstrip()
作用:去除两边空格、左边空格、右边空格
s = " abcd "
print("|"+s.strip()+"|")
print("|"+s.lstrip()+"|")
print("|"+s.rstrip()+"|")
查看运行结果:

2、removeprefix()、removesuffix()
作用:移除前缀、移除后缀
s = "hello:world"
print(s.removeprefix("hello"))
print(s.removesuffix("world"))
查看运行结果:

3、replace()
作用:替换字符串中的内容替换成指定的内容
s = "hello:world"
s = s.replace(":", "-")
print(s)
查看运行结果:

4、split()、rsplit()
作用:从左边起根据对用的内容分割字符串、从右边起根据对用的内容分割字符串(当指定字符串的分隔次数时存在区别)
s = "hello:world:ok"
print(s.split(":"))
print(s.rsplit(":"))
print(s.split(":", maxsplit=1))
print(s.rsplit(":", maxsplit=1))
查看运行结果:

5、join()
作用:将括号内的元素(均需要满足字符串格式)合并成一个新的字符串,已join前的字符作为分隔符
l = ["hello", "1", "world"]
print("".join(l))
print("-".join(l))
查看运行结果:

6、upper()、lower()、capitalize()
作用:将所有字母转为大写、将所有字母转为小写、将首字母转为大写
s = "helloWORLD"
print(s.upper())
print(s.lower())
print(s.capitalize())
查看运行结果:

7、islower()、isupper()、isalpha()、isnumeric()、isalnum()
作用:检查字符串中字母是否都为小写、检查字符串中字母是否都为大写、检查字符串中字符是否都是字母、检查字符串中字符是否都是数字、检查所有的字符串是否都是数字或字母
s1 = "helloworld"
s2 = "OK"
s3 = "hello OK"
s4 = "567"
s5 = "hello123"
print(s1.islower())
print(s2.isupper())
print(s3.islower(), s3.isupper())
print(s1.isalpha())
print(s4.isnumeric())
print(s5.isalpha(), s5.isnumeric())
print(s5.isalnum())
print(s3.isalnum())
查看运行结果:

8、count()
作用:返回指定内容在字符串中出现的次数
s = "hello world"
print(s.count("o"))
查看运行结果:

9、find()、rfind()
作用:返回字符串中是否包含指定内容的索引信息(从左边开始第一个出现的),不包含时返回-1、返回字符串中是否包含指定内容的索引信息(从右边开始第一个出现的),不包含时返回-1
s = "hello world"
print(s.find("x"))
print(s.find("o"))
print(s.rfind("o"))
查看运行结果:

10、startswith()、endswith()
作用:检查字符串是否是以指定内容开头、检查字符串是否是以指定内容结束
s = "hello world"
print(s.startswith("h"), s.endswith("h"))
print(s.startswith("d"), s.endswith("d"))
查看运行结果:

11、partition()
作用:有点像find()和split()的结合体。将字符串根据指定的内容拆分为三个元素的元祖,其中第二个元素为指定的内容,如果不包含指定的内容的话,返回的第一个元素为原字符串
s = "hello world"
print(s.partition(" "))
print(s.partition("hello"))
print(s.partition("123"))
查看运行

12、center()、ljust()、rjust()
作用:
返回一个原字符串居中,并使用指定内容(默认为空格)填充至长度width的新字符串
返回一个原字符串左对齐,并使用指定内容(默认为空格)填充至长度width的新字符串
返回一个原字符串右对齐,并使用指定内容(默认为空格)填充至长度width的新字符串。
s = "python"
print(s.center(30))
print(s.center(30, "-"))
print(s.ljust(30, "-"))
print(s.rjust(30, "-"))
查看运行结果:

13、字符串专用f表达式
作用:是格式化字符串的新语法,更易读,更简洁,不易出错,而且速度更快!需要python3.6+的版本支持
name = "bk"
age = 15
print(f"my name is {name},and i am {age} years old!")
查看运行结果:

14、swapcase()
作用:翻转字符串中的字母大小写
name = "My Name is Mr.white"
print(name.swapcase())
查看运行结果:

15、zfill()
作用:返回长度为width的字符串,原字符串string右对齐,前面填充0
print("100".zfill(5))
print("+100".zfill(5))
print("-100".zfill(5))
print("+0010".zfill(5))
查看运行结果:

python字符串常用方法介绍,基于python3.10的更多相关文章
- python 字符串常用方法
字符串常用方法 capitalize() String.capitalize() 将字符串首字母变为大写 name = 'xiaoming' new_name = name.capitalize() ...
- Python字符串常用方法(二)
二.字符串的操作常用方法 字符串的替换.删除.截取.复制.连接.比较.查找.分割等 1. string. lower() :转小写 2. string. upper() :转大写 3. string. ...
- python字符串常用方法
#1.strip()去掉空格(字符串首.尾空格).lstrip()去掉左侧空格.rstrip()去掉右侧空格print(' abc '.lstrip())#>>abcprint(' abc ...
- python字符串常用方法、分割字符串等
一.字符串的常用方法 1.str.capitalize() 字符串首字母大写 2.str.center() 把字符串居中 3.str.isalnum() 判断字符串是否含有英文.数字,若有英文和数 ...
- argparse模块入门介绍——基于python3.7
转载:https://blog.csdn.net/weixin_41796207/article/details/80846406 首先说明,本人是想要学习如何使用argparse模块,打造命令行程序 ...
- Python字符串常用方法(一)
一.字符串的判断常用方法 字符串的字母,数字,大小写,空格等的判断 1.string. isalnum() :(字母数字判断) 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 Tr ...
- Python 字符串常用方法 day2
1.去空格和换行符: s = ' a bc ' print(s.strip())#strip(),去掉字符串两边的空格和换行符,无法去除中间的空格 print(s.rstrip())#rstrip() ...
- Python 字符串常用方法总结
明确:对字符串的操作方法都不会改变原来字符串的值 1,去掉空格和特殊符号 name.strip() 去掉空格和换行符 name.strip('xx') 去掉某个字符串 name.lstrip() ...
- python 字符串 常用方法
name = 'ALLix9' print(name.casefold()) # 大写变成小写 name.lower() # 全变小写 '.isnumeric()) #判断是否是数字:正整数 prin ...
- python基础(2)字符串常用方法
python字符串常用方法 find(sub[, start[, end]]) 在索引start和end之间查找字符串sub 找到,则返回最左端的索引值,未找到,则返回-1 start和end都可 ...
随机推荐
- phpoffice文档笔记
目录 phpword html转word phpexcel 从数据库导出 phpword html转word <?php namespace app\index\controller; use ...
- 分布式安装部署MinIO
官方文档地址:http://docs.minio.org.cn/docs/master/distributed-minio-quickstart-guide 前提条件:分布式Minio至少需要4个硬盘 ...
- Centos7搭建 KVM 环境
注意:使用KVM管理虚拟机分命令行和图形化界面两种方式,这里最后一步安装的工具是图形化管理工具 安装前准备 验证CPU是否支持KVM 如果结果中有vmx(Intel)或svm(AMD)字样,就说明CP ...
- 使用KubeOperator安装k8s集群后,节点主机yaml文件路径
[root@k8s-develop-master-1 kubernetes]# cd /etc/kubernetes [root@k8s-develop-master-1 kubernetes]# l ...
- 驱动开发:内核CR3切换读写内存
首先CR3是什么,CR3是一个寄存器,该寄存器内保存有页目录表物理地址(PDBR地址),其实CR3内部存放的就是页目录表的内存基地址,运用CR3切换可实现对特定进程内存地址的强制读写操作,此类读写属于 ...
- 重学Java泛型
一丶从字节码层面看范型擦除 public class Type1<T> { private T t; } 使用jclasslib插件查看其字节码: 可以看到 t属性的类型是List< ...
- 案例分享 生产环境逐步迁移至k8s集群 - pod注册到consul
#案例分享 生产环境逐步迁移至k8s集群 - pod注册到consul #项目背景 多套业务系统, 所有节点注册到consul集群,方便统一管理 使用consul的dns功能, 所有节点hostnam ...
- ERP是什么呢?
ERP(Enterprise Resource Planning,企业资源计划)系统,是进行物质资源.资金资源和信息资源集成一体化管理的企业信息管理系统,ERP统领企业全局,为管理层服务,重心在于企业 ...
- postman7种断言的使用
导航:1.postman断言介绍2.状态码断言3.响应正文断言-包含4.响应正文断言-json字段5.响应头断言6.响应时间断言7.环境变量的断言 - - - - - - - - - 分割线 - - ...
- P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并 (树上差分+线段树合并)
显然的树上差分问题,最后要我们求每个点数量最多的物品,考虑对每个点建议线段树,查询子树时将线段树合并可以得到答案. 用动态开点的方式建立线段树,注意离散化. 1 #include<bits/st ...