字符串函数主要分为:

查找:

 strs = "this is a line of text for test"

 index1 = strs.find("is")
print(index1)
# 指定范围,指定起始结束位置
index2 = strs.find("is", 0, 15)
print(index2)
# find 查找不存在,返回-1,返回第一个字符索引值
print(strs.find("isa"))
# rfind,从右边开始查找
print(strs.rfind("is"))
# index 查找,不存在,会报错
print(strs.index("test"))
# 可以指定起始,结束位置,不包括结束位置
# print(strs.index("test",0,15))
# rindex(),从右边开始查找
print(strs.rindex("is"))
# count(str,start,end)返回str在start,end出现的次数,不包括结束位置
print(strs.count("is", 0, 4))
print(strs.count("is"))

替换

# replace(old,new,count)
# 把字符串中的old替换成new,count指定替换的最大次数,替换次数<=count,不指定默认替换所有
# 返回替换后的字符串
print(strs.replace("is", "SS", 4))
print(strs.replace("is", "SS"))
print(strs)

字母处理(大小写转换):

 # capitalize(),将字符串第一个字符大写,返回替换后新的字符串
str2 = "this is a test for a line text"
str3 = str2.capitalize()
print(str3)
# title(),将字符串的每个单词首字母大写,返回替换后新的字符串
str4 = str2.title()
print(str4)
# lower(),将字符串的大写字母替换成小写字母,返回
str5 = "THIS IS A TEST TO A LINE OF text"
str6 = str5.lower()
print(str6)
# upper() ,将字符串的小写字母替换成大写字母,返回
print(str6.upper())
# swapcase() # 大小写互换
str6="This IS A Test"
print("str6",str6.swapcase())

字符串格式化对齐相关:

#ljust(width,fillstr),左对齐
#rjust(width,fillstr),右对齐
#center(width,fillstr),居中
#默认用空格填充指定宽度,可以指定字符串,返回填充后的字符串
print("this is a title".center(52,"-"))
print("|","|".rjust(51))
print("|","this is text".center(51,"*"),"|",sep="")
print("|".ljust(51," "),"|")
print("-"*53)
字符串去空格及去指定字符
# strip(),删除字符串两端的空白字符,可以指定删除的字符
# lstrip(),删除字符串左边的空白字符,可以指定删除的字符
# rstrip()#删除字符串末尾的空白字符,可以指定删除的字符
str7 = " this is a test for blank "
print(str7)
print(str7.strip())
print(str7.lstrip())
print(str7.rstrip())

字符串切割

# split(sp,maxsplit) ,把字符串以sp切割符(默认空格)切割,maxsplit指定切割次数,
# 返回切割后字符串,组成的列表
str1 = "张三|23|180|58"
fields = str1.split("|")
fields = str1.split("|", 2)
print(fields)
str8="""
我是第一个行
我是第二行
我是第三行
我是第四行
我是第五行
"""
# splitlines(keepends) ,按照行分隔,返回一个包含各行作为元素的列表
# keepends,True显示\n,False不显示\n
lines = str8.splitlines(True)
for i in lines:
print(i)

partition() 分区

  • str1.partition("sep")
  • 以sep拆分,放到元组中
  1. sep第一次出现时分割,返回一个包含分割符前部分、分割符本身和分割符后部分三部分组成的tuple

如:

str1 = "abccdc"
print(str1.partition("c"))

运行结果:

('ab', 'c', 'cdc')

字符串判断相关

  • (4)islower():判断字符串是否以纯小写字母组成

  • (5)isupper():判断字符串是否以纯大写字母组成

  • (6)isalpha():判断字符串是否全为字母(汉字属于字母范围)

  • (7)isalnum():判断字符串是否全为字母和数字

  • (8)isspace():判断字符串是否全为空格

  • (9)istitle():判断字符串是否为标题格式(每个单词首字母大写)

# startswith(),检查字符串是否是以指定字符串开头,是,返回True,否则,返回False
# 可以加指定起始结束位置,
str2 = "this is a test for a line text"
bo = str2.startswith('th')
print(str2.startswith('this', 0, 3))
print(bo) # endswith(suffix,start,end),检查字符串是否是以指定字符串开头,是,返回True,否则,返回False
# 可以加指定起始结束位置,
bo1 = str2.endswith("text")
print(bo1) # isdigit()
# True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
# False: 汉字数字
# Error: 无
#
# isdecimal()
# True: Unicode数字,,全角数字(双字节)
# False: 罗马数字,汉字数字
# Error: byte数字(单字节)
#
# isnumeric()
# True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
# False: 无
# Error: byte数字(单字节) #isalpha(),如果字符串中所有字符都是字母 则返回 True,否则返回 False
#当字符串设置为utf-8,不能检测中文
str9 = "ia ma litttl ejoy"
print(str9.isalpha())# False,包含空格
str9 = "hahhayoudontseemewhatimeans"
print(str9.isalpha())#True
# isdigit(),如果字符串中只包含数字则返回 True 否则返回 False.
str10 = ""
print(str10.isdigit())#True
str10 = "1234567a89043"
print(str10.isdigit())#False
str10 = "1234 56789043 "
print(str10.isdigit())#False
#isalnum(),如果字符串中所有字符都是字母(中文)或数字则返回 True,否则返回 False
print("----"*39)
str11 = "1234567a89043"
str11 = u"1234567a89043咋呼大".encode("utf-8")
# isnumeric() 检测字符串中的数字,纯数字(中文的数字(一,壹,二贰),罗马数字ⅡⅢⅣⅤ)返回True,
# str11 = '12233一二三叁④'
# print(str11.isnumeric())
print(str11.isalnum())
print(str11.isalpha())
print("一壹二贰ⅡⅢⅣⅤ₃⅔❽⒇".isnumeric())
#isspace(), 如果字符串中只包含空格,则返回 True,否则返回 False.
# print("\t\n ".isspace()) True

python字符串函数总结的更多相关文章

  1. 【C++实现python字符串函数库】strip、lstrip、rstrip方法

    [C++实现python字符串函数库]strip.lstrip.rstrip方法 这三个方法用于删除字符串首尾处指定的字符,默认删除空白符(包括'\n', '\r', '\t', ' '). s.st ...

  2. 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith

    [C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...

  3. 【C++实现python字符串函数库】一:分割函数:split、rsplit

    [C++实现python字符串函数库]split()与rsplit()方法 前言 本系列文章将介绍python提供的字符串函数,并尝试使用C++来实现这些函数.这些C++函数在这里做单独的分析,最后我 ...

  4. 【276】◀▶ Python 字符串函数说明

    参考:Python 字符串函数 01   capitalize 把字符串的第一个字符大写,其他字母变小写. 02   center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串. ...

  5. (转)python字符串函数

    原文:https://www.cnblogs.com/emanlee/p/3616755.html https://blog.csdn.net/luoyhang003/article/details/ ...

  6. Python 简明教程 --- 8,Python 字符串函数

    微信公众号:码农充电站pro 个人主页:https://codeshellme.github.io 好代码本身就是最好的文档.当你需要添加一个注释时,你应该考虑如何修改代码才能不需要注释. -- St ...

  7. 在 R 中使用 Python 字符串函数

    sprintf( )函数很强大,但并非适用于所有应用场景.例如,如果一些部分在模板中多次出现,那么就需要多次写一样的参数.这通常会使得代码冗长而且难以修改:sprintf("%s, %d y ...

  8. python 字符串函数

    split函数:将字符串分割成序列 str.split("分隔符") 一般可以这样用 list = [n  for n in str.split],这样可以得到一个新的序列 str ...

  9. python 字符串函数功能快查

    0.dir(str)一.有字符发生转换1.capitalize,字符串的第一个字符大写2.casefold,将所有字符小写,Unicode所有字符均适用3.lower,将所有字符小写,只适用ASCii ...

随机推荐

  1. linear-gradient

    http://jsbin.com/mocojehosa/edit?html,css,output https://developer.mozilla.org/zh-CN/docs/Web/CSS/li ...

  2. Extjs4 的一些语法 持续更新中

    一.给GridPanel增加成两行toolbar tbar: { xtype: 'container', layout: 'anchor', defaults: {anchor: '0'}, defa ...

  3. Java虚拟机原理图解-- 1.2、class文件中的常量池

    了解JVM虚拟机原理 是每一个Java程序员修炼的必经之路.但是由于JVM虚拟机中有很多的东西讲述的比较宽泛,在当前接触到的关于JVM虚拟机原理的教程或者博客中,绝大部分都是充斥的文字性的描述,很难给 ...

  4. PAT甲级——A1010 Radix

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  5. PAT甲级——A1031 Hello World for U

    Given any string of N (≥) characters, you are asked to form the characters into the shape of U. For ...

  6. Python移动自动化测试面试

    Python移动自动化测试面试 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可以关 ...

  7. springboot整合mybatis通用Mapper

    参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451d ...

  8. Vuejs实战项目步骤一

    1.使用vue初始化项目 vue create msm-demo #创建项目 npm run serve #部署 2.更改public文件夹下面的index文件,只留下 <div id=&quo ...

  9. 理解 Python 语言中的 defaultdict

    众所周知,在Python中如果访问字典中不存在的键,会引发KeyError异常(JavaScript中如果对象中不存在某个属性,则返回undefined).但是有时候,字典中的每个键都存在默认值是非常 ...

  10. [Array]448. Find All Numbers Disappeared in an Array

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...