32 字符串常用的方法 center find join s
发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,第八课 字符串中常用的方法:center方法 # 字符串方法:center# 作用是:将字符串在一定的宽度区域内居中显示# 这个方法和我们之前将的format 中的 ^ 一样 # ^print("<
千家信息网最后更新 2025年02月05日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安全错误
数据库的锁怎样保障安全
项目上传服务器
失去了数据库安装教程
数据库索引hash
涪陵区常规软件开发流程常见问题
呼市短期app软件开发
重庆服务器
南通云服务器哪个厂家质量好
金仓数据库查看用户
专科智能互联网络技术
南京市番薯网络技术有限公司
服务器查看raid管理器
广州华磊网络技术
农村信用信息基础数据库建设意见
行唐县网络安全委员会
数据库占用的内存越来越大
在哪里看dns服务器地址
对网络安全审查意见
软件开发招
公安部身份证数据库
在家接软件开发的活
网络安全对策问卷
服务器系统升级影响业务吗
恩施克奇网络技术有限公司
社区要入户进行网络安全
盘州网络安全系统哪家靠谱
自动化设计数据库
两会关于网络安全通知
csgo服务器发龙狙
数据库设置当前命令
数据库5v