Python3中有哪些常用字符串操作
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,这期内容当中小编将会给大家带来有关Python3中有哪些常用字符串操作,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1. str.strip去掉字符串首位指定的字符
千家信息网最后更新 2024年09月22日Python3中有哪些常用字符串操作
这期内容当中小编将会给大家带来有关Python3中有哪些常用字符串操作,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
1. str.strip去掉字符串首位指定的字符,默认去掉空白字符
intput=" nnn入门小站nnn "print('[%s]' % input)# strip不会改变原字符串的值,所以需要一个新变量接收newInput=input.strip()print('[%s]' % newInput)# 去掉首位的n字符print('[%s]' % input.strip('n'))# 去掉首位的n字符+空白字符print('[%s]' % input.strip('n').strip())
[ 入门小站 ][入门小站][ 入门小站 ][入门小站]
2. str.center使用指定字符对字符串进行对齐
intput="入门小站"print(input.center(12,'#'))
### 入门小站 ###
3. str.count()统计字符串再另外一个字符串中出现的次数
# 语法 str.count(value, start, end) # start 起始索引 可选# end 结束索引 可选input="入门 入门小站 入门 rumen 入门"print(input.count('入门'))# 指定查找范围print(input.count('入门',2,7))
41
4. str.find() 查找字符串首次出现的位置
# 语法 str.find(value,start,end)# start 起始索引 可选# end 结束索引 可选# 如果没有找到则返回-1input="入门 入门小站 入门 rumen 入门"print(input.find('入门'))# 指定范围print(input.find('入门',2,7))
03
5. str.rfind(value,start,end) 查找字符串最后出现的位置
# 语法 str.rfind(value,start,end)# start 起始索引 可选# end 结束索引 可选# 如果没有找到则返回-1input="入门 入门小站 入门 rumen 入门"print(input.rfind('入门'))# 指定范围print(input.rfind('入门',2,7))
173
6. str.swapcase()返回一个字符串的副本,并且对字符串进行大小写转换
input=" ru men XIAO zhan "print(input.swapcase())
RU MEN xiao ZHAN
7. str.startswith()判断字符串是不是以某个字符串开始
# 语法:str.startswith(prefix[, start[, end]]) -> bool# start 起始索引 可选# end 结束索引 可选# 返回bool值input="入门小站"print(input.startswith('入门'))# 指定范围print(input.startswith('入门',2))
TrueFalse
8.str.endswith()判断字符串是不是以某个字符串结尾
# str.endswith(suffix[, start[, end]]) -> bool# start 起始索引 可选# end 结束索引 可选# 返回bool值input="入门小站"print(input.endswith('小站'))# 指定范围print(input.endswith('小站',2))
TrueTrue
9. str.split() 分割字符串成一个list,默认分割符是任意数量的空白字符
# 语法:str.split(self, /, sep=None, maxsplit=-1)input=" n 入门 n 小站 n "print(input.split())# 指定分割字符print(input.split('n'))
['n', '入门', 'n', '小站', 'n'][' ', ' 入门 ', ' 小站 ', ' ']
10. 字符串大小写转换
# 10.1 str.capitalize() 将字符串的第一个字符转成大写input="ru mEn z "print(input.capitalize())# 10.2 str.upper() 将所有字符转换成大写print(input.upper())# 10.3 str.title() 将每个单词的首字符转换成大写,其余字符转换成小写print(input.title())
Ru men z RU MEN Z Ru Men Z
11. str.ljust()和str.rjust()字符串首位用指定的字符填充到指定长度,默认以空格填充
# 语法: ljust(self, width, fillchar=' ', /)# 语法: rjust(self, width, fillchar=' ', /)# width 为填充后的字符串的长度input="rumenz"print(intput.ljust(20,'#'))print(input.rjust(20,'$'))
入门小站################$$$$$$$$$$$$$$rumenz
12. str.zfill() 字符串前面填充0
# 语法:str.zfill(self, width, /)# width 字符串填充后字符串的长度input="rumenz"print(intput.zfill(20))
0000000000000000入门小站
13. 定义一个多行字符串
input='''入门小站rumenz'''print(input)
入门小站rumenz
14. 判断字符串是否在另外一个字符串中
ints="ru men z "print('one' in ints)
False
15. is判断字符串地址时候相同
str1="rumenz"str2="rumenz"print(str1 is str2)
True
16. 判断字符串的类型
# str.isalnum() 判断字符串是不是由字母和数字组成str1="rumenz123"print(str1.isalnum())# str.isalpha() 判断字符串是不是由字母组成print(str1.isalpha())# str.isdigit() 判断字符串是不是纯数字print(str1.isdigit())
TrueFalseFalse
17. 数组拼接成字符串
# 语法 "".join(str)li=['入门','小站','123']print(''.join(li))print('-'.join(li))
入门小站123入门-小站-123
18. 字符串替换
# 语法 str.replace(self, old, new, count=-1, /)# old 旧字符串# new 要替换成的新字符串# count 替换的次数,默认为-1 ,全部替换str1="入门小站入门"print(str1.replace("入门","rumen"))
rumen小站rumen
19. 字符串映射
# a->1 b->2 c->3 d->4 e->5 f->6p=str.maketrans('abcdef','123456')print('aaacccd'.translate(p))
1113334
上述就是小编为大家分享的Python3中有哪些常用字符串操作了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。
字符
字符串
小站
索引
语法
范围
起始
大写
空白
长度
常用
位置
内容
大小
字母
数字
次数
分析
相同
专业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
浙江云客网络技术
sqlite建数据库
软件开发销售收入吗
山东统一软件开发设施参考价格
改变网络安全类型
芯能科技电力互联网
mt管理器我的世界服务器
宜良方便软件开发价格信息
英特尔的服务器芯片咋样
喵眼精灵服务器繁忙
lol服务器有多少
易用软件开发
电脑怎么做数据库题目
初中生学软件开发的学校
您的网络安全流量
管理服务器及软件
导入数据库错误114
服务器怎么选择上网路由器
为什么要用数据库管理
c 软件开发招工信息6
爱奇艺c 软件开发
阿里服务器租用
所谓数据库访问 它要求
lol手游转服务器多少钱
桓台快消品软件开发公司
网络技术与应用期末总结
网络安全服务提供
服务器断电后数据库丢失了
金融软件开发工资待遇
湖南自动量化交易软件开发费用