Python字符串(string)常用函数

您所在的位置:网站首页 连接字符串的函数python Python字符串(string)常用函数

Python字符串(string)常用函数

2024-07-11 02:38:09| 来源: 网络整理| 查看: 265

Python字符串(string)常用函数

find:检测字符串是否包含指定字符,如果存在则返回开始的索引值,否则返回-1

str1 = 'hello world' print(str1.find('w')) #存在,则返回是该字符串位置 print(str1.find('z')) #不存在,则返回-1 返回结果: 6 -1

index:检测字符串是否包含指定字符,若果存在返回开始的索引值,否则提示错误信息

str1 = 'hello world' print(str1.index('d')) #存在,则返回该字符串位置 print(str1.index('z')) #不存在,提示错误信息 返回结果: 10 print(str1.index('z')) #不存在,提示错误信息 ValueError: substring not found

count:返回字符串指定索引范围内[start,end]出现的次数

str1 = 'hello world' print(str1.count('o')) #统计该字符串出现的次数 print(str1.count('l',2,10)) #指定位置查找,指定第2到第10位字符串之间 返回结果: 2 3

replace:替换,将str1替换成str2,如果指定count,则不超过count次

str1 = 'hello world hello python' print(str1.replace('hello','go out')) #将hello替换为go out 返回结果: go out world go out python

split:切割,指定值进行切割

str1 = 'hello world hello python hello china hello hello hello hello' print(str1.split('o')) #指定字符进行切割,默认输出到列表中 print(str1.split('o',3)) #指定字符串进行切割,从指定位置之后的不进行切割 返回结果: ['hell', ' w', 'rld hell', ' pyth', 'n hell', ' china hell', ' hell', ' hell', ' hell', ''] ['hell', ' w', 'rld hell', ' python hello china hello hello hello hello']

capitalize:将字符串的首字母大写

str1 = 'hello world hello python' print(str1.capitalize()) 返回结果: Hello world hello python

title:将字符串中每个单词的首字母进行大写

str1 = 'hello world' print(str1.title()) #将每个单词的首字母大写 返回结果: Hello World

startswith:检查字符串是否是指定字符开头,是则返回True,否则返回False

str1 = 'hello world hello china' print(str1.startswith('hello')) #是,则返回True print(str1.startswith('world')) #否,则返回False 返回结果: True False

endswith:检查字符串是否是指定字符结尾,是则返回True,否则返回False

str1 = 'hello world hello china' print(str1.endswith('china')) #是,则返回Ture print(str1.endswith('hello')) #否,则返回False 返回结果: True False

lower:将字符串转换为小写

str1 = 'Hello World HELLO CHINA' print(str1.lower()) #将字符串转换为小写 返回结果: hello world hello china

upper:将字符串转换为大写

str1 = 'hello world hello china' print(str1.upper()) #将字符串转换为大写 返回结果: HELLO WORLD HELLO CHINA

ljust:返回一个字符串左对齐,并使用空格填充至长度width的新字符串

str1 = 'hello' print(str1.ljust(10)) #左对齐,字符右侧由空格填充至指定长度 返回结果: hello #选中查看效果

rjust:返回一个字符串右对齐,并使用空格填充至长度width的新字符串

str1 = 'hello' print(str1.rjust(10)) #右对齐,字符左侧由空格填充至指定长度 返回结果: hello #选中查看效果

center:返回一个源字符串居中,并使用空格填充至长度width的新字符串

str1 = 'hello' print(str1.center(15)) #居中对齐,字符左右两侧由空格填充至指定长度 返回结果: hello #选中查看效果

lstrip:去除字符串左边空白字符

str1 = ' hello' print(str1) #默认输出字符串结果左边携带空格字符 print(str1.lstrip()) #去除字符串左边空格后结果 返回结果: hello hello

rstrip:去除字符串右边空白字符

str1 = 'hello ' print(str1) #默认输出字符串结果右边携带空格字符 print(str1.rstrip()) #去除字符串右边空格后结果 返回结果: hello #默认情况 hello

strip:去除字符串两遍空白字符

str1 = ' hello ' print(str1) #默认输出字符串结果左右两遍携带空格字符 print(str1.strip()) #去除后字符串左右两遍空格后结果 返回结果: hello #默认情况 hello

partition:可以根据指定将字符串进行分割,成为三个部分

str1 = 'hello world hello china' print(str1.partition('world')) #指定字符串进行分割为三部分 返回结果: ('hello ', 'world', ' hello china')

join:在str2中每个两个字符中间面插入str1中内容,构造出一个新的字符串

str1 = '_' str2 = ['hello','world','hello'] #在每两个字符中间插入一个str1中内容,使之组成新的字符 print(str1.join(str2)) 返回结果: hello_world_hello

isspace:如果str1中只包含空格,则返回True,否则返回False

str1 = ' ' str2 = ' hello' print(str1.isspace()) print(str2.isspace()) 返回结果: True False

isdigit:如果str1只包含数字则返回True,否则返回False

str1 = 'a123' str2 = '123' print(str1.isdigit()) #返回False,包含字母 print(str2.isdigit()) #返回True,只包含数字 返回结果: False True

isalnum:如果str1所有字符都是字母或数字则返回True,否则返回False

str1 = 'a123' print(str1.isalnum()) #返回True,只包含数字 返回结果: True

isalpha:如果str1所有字符都是都是字母,则返回True,否则返回False

str1 = 'abc' print(str1.isalpha()) #返回True,只包含字母 返回结果: True


【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭