一、常用操作
以字符串
'lstr = 'welcome to Beijing Museumitcpps fdsfs'
为例,介绍字符常见的操作。
find
检测 str 是否包含在 lstr中,如果是返回开始的索引值,否则返回-1。
语法:
lstr.find(str, start=0, end=len(lstr))
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.find("Museum")) print(lstr.find("dada"))
运行结果:
index
跟find()方法一样,只不过如果str不在 lstr中会报一个异常。
语法:
lstr.index(str, start=0, end=len(lstr))
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.index("dada"))
运行结果:
count
返回 str在start和end之间 在 lstr里面出现的次数
语法:
lstr.count(str, start=0, end=len(lstr))
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.count("s"))
运行结果:
replace
把 lstr 中的 str1 替换成 str2,如果 count 指定,则替换不超过 count 次.
1str.replace(str1, str2, 1str.count(str1))
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.replace("s", "ttennd"))
运行结果:
split
以 str 为分隔符切片 lstr,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
1str.split(str=" ", 2)
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.split("to", 5))
运行结果:
capitalize
把字符串的第一个字符大写。
lstr.capitalize()
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.capitalize())
运行结果:
title
把字符串的每个单词首字母大写。
>>> a = "hello itcast" >>> a.title() 'Hello Itcast' #运行结果
startswith
检查字符串是否是以 obj 开头, 是则返回 True,否则返回 False
1str.startswith(obj)
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.startswith('we'))
运行结果:
endswith
检查字符串是否以obj结束,如果是返回True,否则返回 False.
1str.endswith(obj)
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.endswith('hfs'))
运行结果:
lower
转换 lstr 中所有大写字符为小写
1str.lower()
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.lower())
运行结果:
upper
转换 lstr 中的小写字母为大写
1str.upper()
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.upper())
运行结果:
strip
删除lstr字符串两端的空白字符。
>>> a = "\n\t itcast \t\n" >>> a.strip() 'itcast' #运行结果
rfind
类似于 find()函数,不过是从右边开始查找。
1str.rfind(str, start=0,end=len(1str) )
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.rfind('eijing'))
运行结果:
rindex
类似于 index(),不过是从右边开始。
1str.rindex( str, start=0,end=len(1str))
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.rindex('eijing'))
运行结果:
partition
把lstr以str分割成三部分,str前,str和str后。
1str.partition(str)
例:
lstr = 'welcome to Beijing Museumitcpps fdsfs' print(lstr.partition('eijing'))
运行结果:
join
mystr 中每个字符后面插入str,构造出一个新的字符串。
lstr = 'welcome to Beijing Museumitcpps fdsfs' str='233' lstr.join(str) li=["my","name","is","LY"] print(str.join(li))
运行结果:
二、总结
本文详细的讲解了Python基础 ( 字符串 )。介绍了有关字符串,切片的操作。下标索引。以及在实际操作中会遇到的问题,提供了解决方案。希望可以帮助你更好的学习Python。
以上是盘点Python字符串常见的16种操作方法的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。

本文讨论了Python中的单位测试,其好处以及如何有效编写它们。它突出显示了诸如UNITSEST和PYTEST等工具进行测试。

文章讨论了Python的\ _ \ _ Init \ _ \ _()方法和Self在初始化对象属性中的作用。还涵盖了其他类方法和继承对\ _ \ _ Init \ _ \ _()的影响。

本文讨论了python中@classmethod,@staticmethod和实例方法之间的差异,详细介绍了它们的属性,用例和好处。它说明了如何根据所需功能选择正确的方法类型和DA

Inpython,YouAppendElementStoAlistusingTheAppend()方法。1)useappend()forsingleelements:my_list.append(4).2)useextend()orextend()或= formultiplelements:my_list.extend.extend(emote_list)ormy_list = [4,5,6] .3)useInsert()forspefificpositions:my_list.insert(1,5).beaware


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3汉化版
中文版,非常好用

Dreamweaver CS6
视觉化网页开发工具

记事本++7.3.1
好用且免费的代码编辑器

WebStorm Mac版
好用的JavaScript开发工具