32 字符串常用的方法 center find join s
发表于:2024-10-04 作者:千家信息网编辑
千家信息网最后更新 2024年10月04日,第八课 字符串中常用的方法:center方法 # 字符串方法:center# 作用是:将字符串在一定的宽度区域内居中显示# 这个方法和我们之前将的format 中的 ^ 一样 # ^print("<
千家信息网最后更新 2024年10月04日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安全错误
数据库的锁怎样保障安全
安全教育教案网络安全
网络安全审查涉及到外企
aix服务器运行多少个进程
徐州软件开发公司在哪里
网络安全等级保护制度3.0
如何实现网络安全机制
数据库时间加Z表示什么
重庆财务软件开发代码
图书馆怎么开发数据库
达芬奇机器人应用数据库
剑网三 招式数据库
婚恋软件开发多少钱
共产党员网络安全
网络安全法知识竞赛题及答案
音频怎么到数据库
广播电视网络安全规划建议
怎么做不用数据库的网站
郑州专业软件开发定制
重庆合川免费生鲜软件开发
软件开发经理工资3万
数据库管理基础教程答案
小学生网络安全的保障措施
网络安全沙箱分析机
公司服务器防护方案
广州众人互联网科技
淘宝的网络安全技术
剑网三 招式数据库
家庭服务器网络机柜
开展网络安全主题活动
网络安全风险隐患通报