python2之字符串操作
2018-03-19 14:26
183 查看
更新一篇python字符串操作函数,未经允许切勿擅自转载。
字符串拼接:a+b
代码:
![](//img-blog.csdn.net/20180319135855180?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
获取字符串长度:len(str)
结果:
获取字符串的第几个:str[i]
代码:
获取字符串的最后一个
代码:
![](//img-blog.csdn.net/20180319140723475?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
字符串切片:获取字符串中的第a个到第b个,但不包括第b个,c是步长(默认1) str[a:b:c]
代码:
![](//img-blog.csdn.net/2018031914085478?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
字符串倒序
代码:
![](//img-blog.csdn.net/20180319141127486?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
查找字符串,返回查找到的第一个目标下标,找不到返回-1:str.find("s")
代码:
![](//img-blog.csdn.net/20180319141310598?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
统计字符串中,某字符出现的次数:str.count("s")
代码:
![](//img-blog.csdn.net/20180319141442268?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
字符串替换:str.replace(目标字符,替换成的字符)
代码:
![](//img-blog.csdn.net/20180319141647431?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
字符串分割:str.split("s")
代码:
字符串全部变小写:str.lower()
代码:
字符串全部变大写:str.upper()
代码:
字符串第一个字符大写:str.capitalize()
代码:
每个单词首字母大写:str.title()
代码:
以xx结尾(文件后缀名判断):file.endswith(str)
代码:
![](//img-blog.csdn.net/20180319142439749?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L2NhcmZnZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
以xx开头:file.startswith(str)
代码:
字符串拼接:a+b
代码:
a = "woshi" b = "carcar96" print a+b #方法1 print "==%s=="%(a+b) #方法2运行结果:
获取字符串长度:len(str)
结果:
str = "woshiasddscv" print(len(str))运行结果:12
获取字符串的第几个:str[i]
代码:
str = "woshiasddscv" print(str[0])运行结果:w
获取字符串的最后一个
代码:
str = "woshiasddscv" print(str[-1]) print(str[len(str)-1])运行结果:
字符串切片:获取字符串中的第a个到第b个,但不包括第b个,c是步长(默认1) str[a:b:c]
代码:
str = "woshiasddscv" print str[2:4] #sh print str[2:-1] #shiasddsc print str[2:] #shiasddscv print str[2:-1:2] #sisdc运行结果:
字符串倒序
代码:
str = "woshiasddscv" print str[-1::-1] #vcsddsaihsow print str[::-1] #vcsddsaihsow运行结果:
查找字符串,返回查找到的第一个目标下标,找不到返回-1:str.find("s")
代码:
str = "woshiasddscv" print str.find("s") #2 print str.find("gg") #-1运行结果:
统计字符串中,某字符出现的次数:str.count("s")
代码:
str = "woshiasddscv" print str.count("s") #3 print str.count("gg") #0运行结果:
字符串替换:str.replace(目标字符,替换成的字符)
代码:
str = "woshiasddscv" print str.replace("s","S") #woShiaSddScv print str #不变 print str.replace("s","S",1) #woShiasddscv print str.replace("s","S",2) #woShiaSddscv运行结果:
字符串分割:str.split("s")
代码:
str = "woshiasddscv" print str.split("s") #['wo', 'hia', 'dd', 'cv']运行结果:['wo', 'hia', 'dd', 'cv']
字符串全部变小写:str.lower()
代码:
str = "HhnuhHUJHfgt" print str.lower() #hhnuhhujhfgt运行结果:hhnuhhujhfgt
字符串全部变大写:str.upper()
代码:
str = "HhnuhHUJHfgt" print str.upper() #HHNUHHUJHFGT运行结果:HHNUHHUJHFGT
字符串第一个字符大写:str.capitalize()
代码:
str = "woshiasddscv" print str.capitalize() #Woshiasddscv运行结果:Woshiasddscv
每个单词首字母大写:str.title()
代码:
str = "hah hsauh" print str.title() #Hah Hsauh运行结果:Hah Hsauh
以xx结尾(文件后缀名判断):file.endswith(str)
代码:
file = "ancd.txt" print file.endswith(".txt") #True print file.endswith(".pdf") #False运行结果:
以xx开头:file.startswith(str)
代码:
file = "ancd.txt" print file.startswith("ancd") #True print file.startswith("ancds") #False运行结果:
相关文章推荐
- Python学习笔记:字符串基本操作
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python之socket实现unix socket及dash字符串操作
- Python字符串操作
- Python字符串操作
- Python 字符串操作(截取/替换/查找/分割)
- Python字符串操作
- python字符串操作分类总结
- python 的字典、列表、字符串操作
- python字符串操作
- 黄聪:Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- python中字符串的format操作
- python字符串截取操作
- python字符串操作
- Python 字符串操作
- Python字符串操作之复制、连接、比较、求长度
- Python中字符串切片操作
- Python----字符串操作与列表使用
- Python 字符串处理操作