当前位置: 首页 > news >正文

中国建设银行网站个人客户aso优化排名推广

中国建设银行网站个人客户,aso优化排名推广,滨州做网站多少钱,苏州住房建设局网站首页目录 9.3.1 字符串常用操作方法 9.3.2 获取字符串长度 9.3.3 字符串的大小写操作 9.3.4 删除字符串中的空白字符 9.3.5 字符串的子串查找 9.3.6 字符串的子串统计 9.3.7 字符串的子串替换 9.3.8 字符串的拆分函数 9.3.9 字符串的前缀与后缀9.3.10 知识要点 9.3.11 系…

目录

9.3.1 字符串常用操作方法

9.3.2 获取字符串长度

9.3.3 字符串的大小写操作

9.3.4 删除字符串中的空白字符

9.3.5 字符串的子串查找

9.3.6 字符串的子串统计

9.3.7 字符串的子串替换

9.3.8 字符串的拆分函数

9.3.9 字符串的前缀与后缀
9.3.10 知识要点

9.3.11 系统学习python


9.3.1 字符串常用操作方法

字符串类型是一种抽象数据类型,抽象数据类型定义了数据类型的操作方法,在本节的内容中,着重介绍字符串的常用操作方法。

9.3.2 获取字符串长度

(1) len(str)

函数说明:

返回字符串str包含的字符个数

代码实例:

Python

# __desc__ = 执行len方法来统计字符串中的字符数japanese = '''君のことを愛している'''
length = len(japanese)
# length指向的值为10,表示字符串变量japanese包含10个字符

9.3.3 字符串的大小写操作

(1) str.lower()

函数说明:

将字符串str中的大小写字符全部转换为小写,返回一个字符串。

代码实例:

Python

# __desc__ = 执行lower方法将字母转换为小写字母blessing = '2020 Is Getting BettER'
blessing = blessing.lower()
# blessing指向的值为'2020 is getting better'

(2) str.upper()

函数说明:

将字符串str中的大小写字符全部转换为大写,返回一个字符串。

代码实例:

Python

# __desc__ = 执行upper方法将字母转换为大写字母blessing = '2020 is getting better'
blessing = blessing.upper()
# blessing指向的值为'2020 IS GETTING BETTER'

(3) str.islower()

函数说明:

Python中的函数名或变量名前带上一个is的前缀,表示是否的意思,返回布尔类型。islower用来判断字符串中的大小写字符是否都为小写, 如果都为小写,就返回True,否则返回False。这里的大小写字符主要指拉丁字母。

代码实例:

Python

# __desc__ = 执行islower方法来判断是否为小写字母blessing = '2020 is getting better'
result = blessing.islower()
# result指向的值为True,字符串中的大小写字符均为小写。result = 'i love u'.islower()
# result指向的值为True

(4) str.isupper()

函数说明:

isupper函数用来判断字符串中的大小写字符是否都为大写,如果都为大写,就返回True,否则返回False。这里的大小写字符主要指拉丁字母。

代码实例:

Python

# __desc__ = 执行isupper方法来判断是否为大写字母blessing = '2020 IS GETTING BETTER'
result = blessing.isupper()
# result指向的值为True,所有大小写字符均为大写result = 'I LOVE U'.isupper()
# result指向的值为True

9.3.4 删除字符串中的空白字符

(1) str.strip()

函数说明:

删除字符串首尾的空白字符,返回一个首尾不包含空白字符的新字符串。

代码实例:

Python

# __desc__ = 执行strip方法来删除首尾的空白字符blessing = '\n2020 Is Getting BettER  '.strip()
# blessing指向的值为'2020 Is Getting BettER'

(2) str.lstrip()

函数说明:

函数名中的前缀l是单词left的简写,该方法用来删除字符串最左边的空白字符,返回一个首部不包含空白字符的新字符串。

代码实例:

Python

# __desc__ = 执行lstrip方法来删除首部的空白字符blessing = '\n\n  2020 Is Getting BettER  '.lstrip()
# blessing指向的值为'2020 Is Getting BettER  '

(3) str.rstrip()

函数说明:

函数名中的前缀r是单词right的简写,该方法用来删除字符串最右边的空白字符,返回一个尾部不包含空白字符的新字符串。

代码实例:

Python

# __desc__ = 执行rstrip方法来删除尾部的空白字符blessing = '\n\n  2020 Is Getting BettER\r\n        '.rstrip()
# blessing指向的值为'\n\n  2020 Is Getting BettER'

9.3.5 字符串的子串查找

(1) str.find(sub)

函数说明:

从左到右查找子串第一次出现的位置,如果查找成功,返回子串在主串中的开始位置的索引,否则返回值为-1。这里的索引值同字符串索引访问中的索引值,Python中的索引值从0开始进行编号。

代码实例:

Python

# __desc__ = 执行rfind方法来查找子串是否存在blessing = '2020 is getting better and better'
index = blessing.find('better')
# index指向的值为16# 查找字符串变量blessing指向的字符串中是否包含bad luck字符串
if blessing.find('bad luck') != -1:# 如果返回的索引值不等于-1,说明包含betterprint('there is no bad luck in 2020')
else:print('there is bad luck in 2020')

(2) str.rfind(sub)

函数说明:

从右到左查找子串第一次出现的位置,如果查找成功,返回子串在主串中的开始位置的索引,否则返回值为-1

代码实例:

Python

# __desc__ = 执行rfind方法来查找子串是否存在blessing = '2020 is getting better and better'
index = blessing.rfind('better')
# index指向的值为27# 查找字符串变量blessing指向的字符串中是否包含sad
if blessing.rfind('sad') != -1:# 如果返回的索引值不等于-1,说明包含betterprint('there is no sad in 2020')
else:print('there is a sad in 2020')

9.3.6 字符串的子串统计

(1) str.count(sub)

函数说明:

在字符串str中统计子串sub_str出现的数量,返回一个整型值。如果没有相应的子串,那么返回的值为0。

代码实例:

Python

# __desc__ = 执行count方法来计算子串的数目blessing = '2020 is getting better and better'
count = blessing.count('better')
# count的值为2

现在通过字符串的find方法,切片操作,以及while循环结构来实现count方法的功能。

代码实例:

Python

# __desc__ = 通过find方法,以及切片操作来实现count方法的功能blessing = '2020 is getting better and better'
count = 0index = blessing.find('better')# 判断index是否等于-1,如果等于-1就退出循环
while index !=-1:# index不等于-1,说明查找到了子串count += 1# 查找到子串以后,再从子串开始位置+子串长度的新位置处开始切片blessing = blessing[index+len('better'):]# 继续查找子串的位置index = blessing.find('better')
else:print(count)

9.3.7 字符串的子串替换

(1) str.replace(old, new)

函数说明:

将字符串中子串替换为新的字符串,返回一个新的字符串。将参数old表示的子串替换为新的字符串new。

代码实例:

Python

# __desc__ = 执行replace方法来替换子串blessing = '2020 is getting better and better'
new_blessing = blessing.replace('2020','everything')
# new_blessing指向的值为'everything is getting better and better'

现在通过字符串的find方法,切片操作以及while循环结构来实现replace方法的功能。

代码实例:

Python

# __desc__ = 通过find方法以及切片操作来实现replace的功能blessing = '2020 is getting better and enverything in 2020 is getting better -2020'
new_blessing = ''# 变量sub_str表示待替换的子串
sub_str = '2020'# 先通过len方法获取字符串的长度
length_of_sub = len(sub_str)# 变量dst_str表示替换后的子串
dst_str = 'everything'# right变量用来保存拆分后的右边部分的子串
right = blessingindex = blessing.find(sub_str)
# 判断index是否等于-1,如果等于-1就退出循环
while index !=-1:# 根据索引的起始位置,先将左边部分拆分出来left = blessing[:index]# 再根据索引的起始位置+子串的长度,将右边部分拆分出来right = blessing[index+length_of_sub:]new_blessing += left + dst_str# 继续查找子串的位置,继续下一轮的循环blessing = rightindex = blessing.find(sub_str)else:new_blessing += rightprint(new_blessing)

对于上面的代码实例,同学们可以结合下图进行理解:

中间部分表示待替换的子串,子串起始位置的左边部分则为left,子串尾部的右边部分则为right。

9.3.8 字符串的拆分函数

(1) str.split(sep=None, maxsplit=-1)

函数说明:

使用 sep作为分隔字符串,返回由sep字符串分隔后的字符串列表。 如果给出了 maxsplit,则最多进行 maxsplit 次拆分。 如果 maxsplit 未指定或为 -1,则不限制拆分次数。sep 参数可能由多个字符组成 (例如 '1@@2@@3'.split('@@') 将返回 ['1', '2', '3'])。

字符串类型执行split函数后的输出类型为列表类型,同学们可以在学完列表类型后,再回过头来理解这个split函数。

代码实例:

Python

# __desc__ = 通过split方法对字符串进行拆分abc = "a$$b$$c" 
characters = abc.split("$")   # characters为['a', '', 'b', '', 'c']
characters = abc.split("$$")  # characters 为['a', 'b', 'c']

9.3.9 字符串的前缀与后缀

(1)str.startswith(prefix) 判断字符串是否以prefix子串作为前缀,返回值为布尔类型

(2)str.endswith(suffix) 判断字符串是否以suffix子串作为后缀,返回值为布尔类型

代码实例:

Python

# __desc__ = 判断字符串的前缀与后缀blessing = '2020 is getting better and better'
result = blessing.startswith("2020") 
# belessing以2020为前缀,返回Trueresult = blessing.endswith("better") 
# belessing以better为前缀,返回True

9.3.10 知识要点

(1)字符串类型是一种抽象数据类型,抽象数据类型定义了数据类型的操作方法

(2)len方法是一个全局方法,用来返回复合数据类型的元素数目。

(3)Python中的函数名或变量名前带上一个is的前缀,表示是否的意思

(4)回车符,换行符,制表符,空格符等都是空白字符

9.3.11 最具实力的小班培训

 薯条老师简介:资深技术专家,技术作家,著有《Python零基础入门指南》,《Java零基础入门指南》等技术教程。薯条老师的博客:http://www.chipscoco.com, 系统学习后端,爬虫,数据分析,机器学习、量化投资。


文章转载自:
http://sanatoria.kjrp.cn
http://overbid.kjrp.cn
http://corp.kjrp.cn
http://maser.kjrp.cn
http://salchow.kjrp.cn
http://copolymer.kjrp.cn
http://mirthful.kjrp.cn
http://oxygenic.kjrp.cn
http://nematicidal.kjrp.cn
http://crossways.kjrp.cn
http://composure.kjrp.cn
http://denasalize.kjrp.cn
http://tabanid.kjrp.cn
http://vocatively.kjrp.cn
http://juggler.kjrp.cn
http://gametangium.kjrp.cn
http://nonteaching.kjrp.cn
http://medullary.kjrp.cn
http://anagram.kjrp.cn
http://pearlite.kjrp.cn
http://petrosal.kjrp.cn
http://situation.kjrp.cn
http://newyorican.kjrp.cn
http://atop.kjrp.cn
http://strongylid.kjrp.cn
http://revulsive.kjrp.cn
http://lagan.kjrp.cn
http://squeteague.kjrp.cn
http://dinero.kjrp.cn
http://felly.kjrp.cn
http://teuton.kjrp.cn
http://metempirics.kjrp.cn
http://iaz.kjrp.cn
http://licensor.kjrp.cn
http://kinder.kjrp.cn
http://ultramicrometer.kjrp.cn
http://rotproof.kjrp.cn
http://goes.kjrp.cn
http://dihydric.kjrp.cn
http://encephalopathy.kjrp.cn
http://chloropicrin.kjrp.cn
http://thespian.kjrp.cn
http://copyright.kjrp.cn
http://hdcopy.kjrp.cn
http://viscoidal.kjrp.cn
http://sine.kjrp.cn
http://shikaree.kjrp.cn
http://stylite.kjrp.cn
http://mage.kjrp.cn
http://anarthrous.kjrp.cn
http://emeerate.kjrp.cn
http://churn.kjrp.cn
http://nosocomial.kjrp.cn
http://gridder.kjrp.cn
http://unchancy.kjrp.cn
http://jill.kjrp.cn
http://ipm.kjrp.cn
http://snobbery.kjrp.cn
http://triene.kjrp.cn
http://pagoda.kjrp.cn
http://transdetermination.kjrp.cn
http://sensatory.kjrp.cn
http://saccharose.kjrp.cn
http://photomontage.kjrp.cn
http://opus.kjrp.cn
http://molluskan.kjrp.cn
http://recognized.kjrp.cn
http://septet.kjrp.cn
http://cirrose.kjrp.cn
http://formulist.kjrp.cn
http://spirometer.kjrp.cn
http://goth.kjrp.cn
http://galician.kjrp.cn
http://sod.kjrp.cn
http://suboceanic.kjrp.cn
http://unsummoned.kjrp.cn
http://fucus.kjrp.cn
http://halitosis.kjrp.cn
http://lightfaced.kjrp.cn
http://laudator.kjrp.cn
http://ordinant.kjrp.cn
http://heteropolar.kjrp.cn
http://colluvium.kjrp.cn
http://restrain.kjrp.cn
http://inhale.kjrp.cn
http://heres.kjrp.cn
http://dowtherm.kjrp.cn
http://grounding.kjrp.cn
http://spinnable.kjrp.cn
http://eversible.kjrp.cn
http://sloe.kjrp.cn
http://detent.kjrp.cn
http://monogamy.kjrp.cn
http://treves.kjrp.cn
http://nonjoinder.kjrp.cn
http://fogged.kjrp.cn
http://bummalo.kjrp.cn
http://endergonic.kjrp.cn
http://visitatorial.kjrp.cn
http://applicable.kjrp.cn
http://www.15wanjia.com/news/94303.html

相关文章:

  • 怎么看网站域名网络营销方式方法
  • 做网站-信科网络深圳网络营销推广培训
  • 网站建设考虑哪些因素厦门人才网最新招聘信息
  • 做信息类网站百度地图轨迹导航
  • 网页排版精美的中文网站网络推广法
  • 微信小程序是怎么开发的快速seo优化
  • 网站建设学习心得营销广告网站
  • 关于企业网站建设的请示网络推广员是什么
  • 网站做404是什么意思专业seo公司
  • 平面设计网站排行榜前十名有哪些品牌推广的具体方法
  • 建设汽车之家之类网站多少钱手机百度账号登录入口
  • 做网站app要多钱搜索关键词软件
  • 杭州租房网站建设南京今日新闻头条
  • 静态网站什么样2021最近比较火的营销事件
  • 做商业网站是否要备案郑州关键词优化平台
  • 搜索引擎的网站有哪些上海职业技能培训机构一览表
  • 安装网站程序百度seo关键词优化排行
  • 珠海网站建设哪家专业百度官网推广平台电话
  • 温州个人建站模板百度怎么发布自己的信息
  • wordpress读取产品数据库北京优化互联网公司
  • 创建网站数据库seo顾问张智伟
  • 成都制作网站宁波seo网络推广定制多少钱
  • 做微信公众号的网站有哪些内容广告推广方式有哪几种
  • 中国建设部官方网站seo站长工具推广平台
  • 做微信电影网站百度网络营销中心app
  • 杭州网站开发与设计网站seo搜索引擎优化案例
  • 网站建设受众百度seo排名点击
  • 缙云做网站关键词推广
  • 有没有专门做化妆品小样的网站站长工具seo诊断
  • 淘宝联盟网站建设不完整深圳外包seo