32 字符串常用的方法 center find join s
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,第八课 字符串中常用的方法:center方法 # 字符串方法:center# 作用是:将字符串在一定的宽度区域内居中显示# 这个方法和我们之前将的format 中的 ^ 一样 # ^print("<
千家信息网最后更新 2025年12月01日32 字符串常用的方法 center find join s
第八课 字符串中常用的方法:center方法 # 字符串方法:center# 作用是:将字符串在一定的宽度区域内居中显示# 这个方法和我们之前将的format 中的 ^ 一样 # ^print("<" + "hello".center(30) + ">") # < hello ># < hello >print("<{:^30}>".format("hello")) # < hello ># print("<" + "hello".center(20, "*") + ">") # <*******hello********>print("<{:*^20}>".format("hello")) # <*******hello********>-------------------------------------------------第九课 字符串中常用的方法:find方法在大字符串中来查找子子字符串 如果找到了,find方法就会返回子子字符串的第一个字符在大字符串中出现的位置 有就是索引 如果未找到,那么find方法就会返回-1find方法有3个参数 第一个是要查找的子字符串 第二个参数是开始索引 第三个参数是结束索引 左闭右开 # 字符串方法:finds = "hello world"print(s.find("world")) # 6 w的位置索引为 6 print(s.find("abc")) # -1 不存在返回-1 print(s.find("o")) # 4 返回第一个 0的索引 print(s.find("o",6)) # 7 第二个0 出现在第7个位置 print(s.find("l",5,9)) # -1print(s.find("l",5,10)) # 9 左闭右开-------------------------------------------------第十课 字符串中常用的方法:join方法# 字符串方法:join# 用于连接序列中的元素,split方法的逆方法list = ["a", "b", "c", "d", "e"]s = '*'print(s.join(list)) # a*b*c*d*eprint("xy".join(list)) # axybxycxydxye# C:\abc\xyz# /abc/xyz# c:\usr\local\nginx\# /usr/local/nginx/# 这个join 在我们 linux 和windows 中很有帮助 不同的操作系统的路径用python代码如何表示: 要表示一个 linux环境的 路径 # 先定义一个元组 dirs = ('','usr','local','nginx','')linuxPath = '/'.join(dirs)print(linuxPath) # /usr/local/nginx/windowPath = ('C:' + '\\'.join(dirs))print(windowPath) # C:\usr\local\nginx\#numList = [1,2,3,4,5,6] # 这个跑出异常 必须要转化成字符串#print("a".join(numList))-------------------------------------------------------第十一课 字符串中常用的方法:split方法 拆开之后为列表 # 字符串方法:split方法 拆分成列表 s1 = "a b c d e f"print(s1.split()) # ['a', 'b', 'c', 'd', 'e', 'f']s2= "a*b*c*d*e"print(s2.split("*")) # ['a', 'b', 'c', 'd', 'e']path = "/usr/local/nginx"pathList = path.split('/')print(pathList) # ['', 'usr', 'local', 'nginx']windowPath = "D:" + "\\".join(pathList)print(windowPath) # D:\usr\local\nginx--------------------------------------------------第十二课 字符串中常用的方法: lower、upper和capwords函数lower: 把所有的英文字母变成小写upper:把所有的英文字母变成大写capwords函数:会将独立的字符串中首字母大写# 字符串方法:lower、upper和capwords函数print("HEllo".lower()) # helloprint("hello".upper()) # HELLOlist = ["Python", "Ruby", "Java","KOTLIN"]if "Kotlin" in list: print("找到Kotlin了")else: print("未找到Kotlin") # 未找到Kotlin 对大小写敏感for lang in list: # 循环遍历一遍 这样把每一个单词都转化为小写 if "kotlin" == lang.lower(): print("找到Kotlin了") # 找到Kotlin了 break;from string import capwordss = "i not only like Python, but also like Kotlin"print(capwords(s))# I Not Only Like Python, But Also Like Kotlin---------------------------------------------------第十三课 字符串中常用的方法: replace方法和strip方法replace方法:将一个字符串中出现的同样的子字符串 替换,这里最常用的就是把所有的控制台输入的空格 替换成空。例如:"""IDE = input('请输入IDE的名字')findIDE = IDE.replace(' ', '').lower() # 把输入的空格转化为空 并且 把输入的值转化为小写 """strip方法:截取前后空格# 字符串方法:replace和strips = "abcdaedf"print(s.replace("a", "12345")) # 12345bcd12345edfprint(s.replace("xyz","aa")) # abcdaedf 不存在的话 就返回原字符串print(" geekori.com ".strip()) # geekori.comprint(" < geekori.com > ".strip()) # < geekori.com > 只会截取前后的空格,不会截取 中间的空格 langList = ["python", "java", "ruby", "scala", "perl"]lang = " python "if lang in langList: print("找到了python")else: print("未找到python") # 未找到pythonif lang.strip() in langList: print("找到了python")else: print("未找到python") # 找到了pythons = "*** $$* Hello * World ***$$$ "print(s.strip(" *$")) # Hello * World " *$" 这里面的三个是或的关系-------------------------------------------------第十四课 字符串中常用的方法:translate方法和maketrans方法# 字符串方法:translate和maketrans# translate:替换单个字符 maketrans方法:转化为字典 s = "I not only like python, but also like kotlin."table = s.maketrans("ak", "*$") # 他的这个意思是把a替换为* k替换为 $print(table) # {97: 42, 107: 36} 先转发为字典 ACLLZ吗值 print(s.translate(table)) # I not only li$e python, but *lso li$e $otlin.table1 = s.maketrans("ak", "*$", " ") # 第三个参数 删除的意思 整体的意思是: 把a 转化为 * ,k 转化为 $ 并删除掉中间的空格 print(table1) # {97: 42, 107: 36, 32: None}print(s.translate(table1)) # Inotonlyli$epython,but*lsoli$e$otlin.# 把所有的 空格后删除了
方法
字符
字符串
常用
空格
索引
参数
小写
输入
三个
函数
字母
意思
位置
大写
字典
就是
路径
英文
不同
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
实现aspx的数据库修改
内科医疗质量安全数据库
网吧里的电脑怎么连接服务器
天津常规软件开发创新服务
僧人网络安全责任书
读数字媒体设计和网络技术好吗
嵌入式软件开发流程分为
建业软件开发
外包软件开发项目管理
单片机软件开发视频教程
企业组网需要服务器吗
orcle数据库定时备份
宁波 网络安全
戴尔服务器质保
碳资产数据库与管理平台
多功能软件开发答疑解惑
vr软件开发软件
旧手机搭建服务器免流
网络安全考核记录
湖北挑水郎网络技术
兴仁网络安全系统收费多少
ocm数据库怎么安装
用SPSS怎么做数据库
数据库按照时间增序
员宝网络技术公司
网络安全监测摄像头家用
如何提高软件开发人员工作效率
家庭服务器怎么连接公司
网络安全设备登录调试方法
销售部数据库