700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python 字符串系列三字符串的拼接拆分和判断【简单易懂 代码可以直接运行 强烈推荐】

Python 字符串系列三字符串的拼接拆分和判断【简单易懂 代码可以直接运行 强烈推荐】

时间:2020-07-14 00:29:29

相关推荐

Python 字符串系列三字符串的拼接拆分和判断【简单易懂 代码可以直接运行 强烈推荐】

Python 字符串系列三字符串的拼接拆分和判断【简单易懂,代码可以直接运行,强烈推荐】

‘’’

字符串的拼接和拆分

#拆分和拼接

在’‘中\是转义字符,只有\才表示一个

‘’’

#以文件路径为例

path = 'E:\\编程\\编程练习\\数据结构作业\\实验二'#汉字可以字节放到\字符后面,但是字母前面必须加两个\\path1='E:\\biancheng\\bianchenglianxi\\数据结构作业\\实验二'print(path1)str = path1.split('\\')print(str)filename = path.split('\\')[1]print(filename)filename = path.split('\\')[-1]#split函数分割后的字符串相当于一个列表,可以用[]来取下标print(filename)#切分次数str2 = path.split('\\',1)print(str2)str3 = path.split('\\',2)print(str3)

#字符串的拼接

#使用+拼接

#使用jion函数进行拼接,‘拼接时的分隔符’.join(字符串)即可完成拼接

a = 'hello'b = 'Yubenben'print(a+' '+b)#用\\拼接成一个路径filename1 = '\\'.join(str)print(filename1)

‘’’

字符串查询

查询某个字符串中的索引是啥。取出来这个索引后就可以通过索引或者切片操作拿出来想要的数据

index©:查询c第一次出现在字符串中的索引位置

查询不到返回valueError

find():查询c第一次出现在字符串的索引位置

查询不到返回-1

‘’’

建议使用find,因为使用index容易产生报错,而find只是返回一个-1

#index( c ):查询c第一次出现在字符串中的索引位置,从左开始查询

#rindex( c ):返回字符在字符串中最后出现的位置

content = '今天天气真不错!'print(content.index('天'))#1#rindex(c):返回字符在字符串中最后出现的位置print(content.rindex('天'))#2

#find©:查询c第一次出现在字符串中的位置

#rfind©:查询c最后一次出现在字符串中的位置

#若查询的字符不在字符串中则返回-1

print(content.find('天'))#1,从左边找第一个字符在字符串中的位置print(content.rfind('天'))#2print(content.find('中'))#-1

‘’’

字符串判断

startwith©判断字符串是否以c开头

endwith©判断字符串是否以c开头

‘’’

作用:可以用于判断输入的网址是否满足http协议,非常的实用

#判断url是否是http协议url = '/'print(url.startswith('http://'))#按照顺序一直往后出现的字符串都算开头if url.startswith('http'):print('是http协议')else:print('不是http协议')#采用字符串分割的方法来判断网址开头是不是httpif url.split('\:')[0]:print('是http协议')else:print('不是Http协议')

endwith可以用来判断文件的格式:

#endwith 一般用于判断文件格式img_url = 'E:\\图片与娱乐\\背景和头像\\杨倩.jpg'print(img_url.endswith('jpg'))if img_url.endswith('jpg'):print('该图片是jpg格式')else:print('该图片不是jpg格式')

字符串判断的基本函数

#isalnum判断字符串是否满足数字+字母

#isnumeric()判断字符串中都是数字

#isaipha()判断字符串中是否都是字母

#islower函数判断字符串中是不是全是小写字母

#isupper函数判断字符串中是否都是大写字母

s1 = '123nm's2 = '1w2w4w5w'#isalnum判断字符串是否满足数字+字母print(s1.isalnum())print(s2.isalnum())#判断字符串由上面组成#isnumeric()判断字符串中都是数字print(s1.isnumeric())#False#isaipha()判断字符串中是否都是字母print(s1.isalpha())#Falses3 = 'hello Yubenben'#islower函数判断字符串中是不是全是小写字母print(s3.islower())#False#isupper函数判断字符串中是否都是大写字母print(s3.isupper())#False#is是否全部是空格s4 = ' 's5 = ''print(s3.isspace())#Falseeprint(s4.isspace())#Trueprint(s5.isspace())#False

点个👍吧,秋梨膏!!

全部的代码如下:

'''字符串的拼接和拆分#拆分和拼接在’‘中\是转义字符,只有\\才表示一个\'''#以文件路径为例path = 'E:\\编程\\编程练习\\数据结构作业\\实验二'#汉字可以字节放到\字符后面,但是字母前面必须加两个\\path1='E:\\biancheng\\bianchenglianxi\\数据结构作业\\实验二'print(path1)str = path1.split('\\')print(str)filename = path.split('\\')[1]print(filename)filename = path.split('\\')[-1]#split函数分割后的字符串相当于一个列表,可以用[]来取下标print(filename)#切分次数str2 = path.split('\\',1)print(str2)str3 = path.split('\\',2)print(str3)#字符串的拼接#使用+拼接a = 'hello'b = 'Yubenben'print(a+' '+b)#用\\拼接成一个路径filename1 = '\\'.join(str)print(filename1)print('==='*20)'''字符串查询查询某个字符串中的索引是啥。取出来这个索引后就可以通过索引或者切片操作拿出来想要的数据index(c):查询c第一次出现在字符串中的索引位置查询不到返回valueErrorfind():查询c第一次出现在字符串的索引位置查询不到返回-1'''#index(c):查询c第一次出现在字符串中的索引位置,从左开始查询#rindex(c):返回字符在字符串中最后出现的位置content = '今天天气真不错!'print(content.index('天'))#1#rindex(c):返回字符在字符串中最后出现的位置print(content.rindex('天'))#2#查询字符不在字符串中的情况if '中' in content:print(content.index('中'))#报错#find(c):查询c第一次出现在字符串中的位置#rfind(c):查询c最后一次出现在字符串中的位置#若查询的字符不在字符串中则返回-1print(content.find('天'))#1,从左边找第一个字符在字符串中的位置print(content.rfind('天'))#2print(content.find('中'))#-1print('==='*20)'''字符串判断startwith(c)判断字符串是否以c开头endwith(c)判断字符串是否以c开头'''#判断url是否是http协议url = '/'print(url.startswith('http://'))#按照顺序一直往后出现的字符串都算开头if url.startswith('http'):print('是http协议')else:print('不是http协议')#采用字符串分割的方法来判断网址开头是不是httpif url.split('\:')[0]:print('是http协议')else:print('不是Http协议')print('==='*20)#endwith 一般用于判断文件格式img_url = 'E:\\图片与娱乐\\背景和头像\\杨倩.jpg'print(img_url.endswith('jpg'))if img_url.endswith('jpg'):print('该图片是jpg格式')else:print('该图片不是jpg格式')print('==='*20)s1 = '123nm's2 = '1w2w4w5w'#isalnum判断字符串是否满足数字+字母print(s1.isalnum())print(s2.isalnum())#判断字符串由上面组成#isnumeric()判断字符串中都是数字print(s1.isnumeric())#False#isaipha()判断字符串中是否都是字母print(s1.isalpha())#Falses3 = 'hello Yubenben'#islower函数判断字符串中是不是全是小写字母print(s3.islower())#False#isupper函数判断字符串中是否都是大写字母print(s3.isupper())#False#is是否全部是空格s4 = ' 's5 = ''print(s3.isspace())#Falseeprint(s4.isspace())#Trueprint(s5.isspace())#False

点个👍吧,秋梨膏!!!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。