python获取字符串首字母_Python字符串常用操作命令

python获取字符串首字母_Python字符串常用操作命令

2023年7月26日发(作者:)

python获取字符串⾸字母_Python字符串常⽤操作命令本代码测试采⽤python3解释器 string = "i love python very much "检查字符串是否包含在string中,如果包含则返回字符串开始的下标(索引),如果不包含则返回-1string = "i love python very much "("love")("study")-1rfind 和find类似,不过是从右侧开始查找,因为有的字符串在原字符串中会有多个,但返回值只有第⼀个,所有rfind是属于右侧优先原则 作⽤和find⼀样,但如果不包含字符串则抛异常(报错),rindex和rfind类似string = "i love python very much "("love")("study")Traceback (most recent call last):File "", line 1, inValueError: substring not 返回字符串在⽬标字符串中出现的次数string = "i love python very much "("o")("python")(" ")("study")e 将字符串中指定的字符串⽤其它字符串进⾏替换string = "i love python very much "result1 = e("python","girl")result1'i love girl very much '#本⾏代码中的2指得是最多使⽤—_替换两个空格,第三个参数不写默认为0result2 = e(" ","_",2)result2'i_love_python very much ' 以指定字符分割切⽚字符串,返回值为list列表,⽆参数默认为空格分割string = "i love python very much "#不写参数默认使⽤空格分割,且连续多个空格视为⼀个#若空格在⾸部或尾部则不再往两端分割result3 = ()result3['i', 'love', 'python', 'very', 'much']result4 = (" ")result4['i', 'love', 'python', 'very', 'much', '']result5 = ("e")result5['i lov', ' python v', 'ry much ']lize 将字符串⾸字母转换成⼤写string = "i love python very much "result6 = lize()result6'I love python very much ' 将字符串中每个单词的⾸写result7 = ()result7'I Love Python Very Much 'ith 检查字符串开头是否包含指定字符串string = "i love python very much "result9 = with("i")result9Trueresult10 = with("a")th 检查字符串结尾是否包含指定字符串,和startswith类似string = "i love python very much "result11 = th(" ")result11Trueresult12 = th("ab") 将字符串中所有的⼤写字母转化为⼩写string = "i love python very much"result13 = ()result13'i love python very much' 将字符串中所有的⼩写字母转化为⼤写,和lower相反的作⽤string = "i love python very much"result14 = ()result14'I LOVE PYTHON VERY MUCH' 返回⼀个原字符串左对齐,并使⽤空格来填充剩余位置的字符串string =" hello"result15 = (10)result15'hello ' 返回⼀个原字符串右对齐,并使⽤空格来填充剩余位置的字符串string =" hello"result16 = (10)result16' hello' 返回⼀个原字符串居中对齐,并使⽤空格来填充剩余位置的字符串string =" hello"result17 = (9)result17' hello '免责声明:内容和图⽚源⾃⽹络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

发布者:admin,转转请注明出处:http://www.yc00.com/news/1690366727a338959.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信