No1.字符串的魔法

#首字母大写
>>> test = "trony"
>>> v = test.capitalize()
>>> print(v)
Trony
#将字符串中大写变小写有两个方法casefold()和lower(),casefold()更加的牛逼,它可以将许多未知的字符串变小写
>>> test = "FDKFJ"
>>> v1 = test.casefold()
>>> print(v1)
fdkfj
>>> v2 = test.lower()
>>> print(v2)
fdkfj
#center()方法设置宽度并将内容居中
>>> test = "trony"
>>> v = test.center(20, "*")
>>> print(v)
*******trony********
#count()计算当前当前字符列的出现次数,后面参数可以指定范围
>>> test = "tronytrony"
>>> v = test.count('o')
>>> print(v)
2
>>> v1 = test.count("o", 5, 6)
>>> print(v1)
0
#endswith()以什么结尾,startswith()以什么开始,可指定范围
>>> test = "trony"
>>> v = test.endswith('a')
>>> print(v)
False
>>> v1 = test.startswith("t")
>>> print(v1)
True
#从开始往后找,找到第一个之后,获取其位置,可指定范围
>>> test = "pythonpython"
>>> v = test.find("on")
>>> print(v)
4
#字符串的格式化,将字符串中的占位符替换为指定的值
>>> test = "i am {name}, age {a}"
>>> v = test.format(name = "trony", a = 19)
>>> print(v)
i am trony, age 19
>>> test1 = "i am {0}, age {1}"
>>> v1 = test1.format("trony", 19)
>>> print(v1)
i am trony, age 19
>>> test2 = "i am {name}, age {a}"
>>> v2 = test2.format_map({"name":"trony", "a":19})#与format()类似不过,format_map()传入的是字典
>>> print(v2)
i am trony, age 19
#寻找字符串,并返回其下标,找不到直接报错
>>> test = "tronytrony"
>>> v = test.index("8")
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    v = test.index("8")
ValueError: substring not found
>>> v1 = test.index("o")
>>> print(v1)
2
#判断该字符串是否只有数字和字母
>>> test = "dsf465"
>>> v = test.isalnum()
>>> print(v)
True
#expandtabs(value)遇到制表符则,以value作为位数开始数,遇到制表符制表符表示剩下的位数
>>> test = "sfsf\tjdfkd\t"
>>> v = test.expandtabs(6)
>>> print(v)
sfsf  jdfkd 
#判断是否只有字母
>>> test = "是trony"
>>> v = test.isalpha()
>>> print(v)
True
>>> 
#判断是否是数字,厉害程度isdecimal()<isdigit()<isnumeric()
>>> test = "123"
>>> v1 = test.isdecimal()
>>> v2 = test.isdigit()
>>> v3 = test.isnumeric()
>>> print(v1, v2, v3)
True True True
#标识符:数字、字母、下划线。isidentifier()判断是否为标识符
>>> a = "123546"
>>> v = a.isidentifier()
>>> print(v)
False
>>> v1 = b.isidentifier()
>>> print(v1)
True
#判断字符串是否包含不可显示的字符如:\t、\n
>>> test = "sfsj\n"
>>> v = test.isprintable()
>>> print(v)
False
>>> test2 = "sdfjkds"
>>> v2 = test2.isprintable()
>>> print(v2)
True
#判断字符串是否是空格
>>> test = "dfdkf"
>>> v = test.isspace()
>>> print(v)
False
>>> test1 = "djfk   "
>>> v1 = test1.isspace()
>>> print(v1)
False
>>> test2 = "    "
>>> v2 = test2.isspace()
>>> print(v2)
True
#istitle()判断字符串是否是标题,title()将字符串转化为标题
>>> test = "trony tiger"
>>> v1 = test.istitle()
>>> print(v1)
False
>>> v2 = test.title()
>>> print(v2)
Trony Tiger
>>> v3 = test.istitle()
>>> print(v3)
False
#将字符串中的每一个元素按照指定的分隔符连接起来
>>> li = ['fd', 'dfd', 'dfdf']
>>> v1 = "_".join(li)
>>> print(v1)
fd_dfd_dfdf
>>> tu = ('fdf', 'fd')
>>> v2 = "_".join(tu)
>>> print(v2)
fdf_fd
#类似于center()
>>> test = "trony"
>>> v = test.ljust(20, "*")
>>> print(v)
trony***************
>>> v1 = test.rjust(20, "*")
>>> print(v1)
***************trony
#islower()与lower()
#isupper()与upper()
>>> test = "Trony"
>>> v = test.islower()
>>> print(v)
False
>>> v1 = test.lower()
>>> print(v1)
trony
>>> v2 = test.isupper()
>>> print(v2)
False
>>> v3 = test.upper()
>>> print(v3)
TRONY
#默认去除两端空白,也可以去处\t、\n
#传入参数时,按照最大匹配原则去除字符串
>>> test = "  trony  "
>>> v1 = test.strip()
>>> v2 = test.lstrip()
>>> v3 = test.rstrip()
>>> print(v1, v2, v3)
trony trony     trony
#使用maketrans()方法构造一个对应关系,使用translate()方法进行替换
>>> v = "fdkfladjafkljaifoagngk"
>>> m = str.maketrans("aeiou", "12345")
>>> new_v = v.translate(m)
>>> print(new_v)
fdkfl1dj1fklj13f41gngk
#分割,使用partitionn()分割以value进行分割可以获得三部分,只能接收一个参数
#使用split()分割以value进行分割,可以接收两个参数
>>> test = "fdfsfdssdgsfbdhsfdgw"
>>> v = test.partition('s')
>>> print(v)
('fdf', 's', 'fdssdgsfbdhsfdgw')
>>> v = test.rpartition('s')
>>> print(v)
('fdfsfdssdgsfbdh', 's', 'fdgw')
>>> v3 = test.split('s', 3)
>>> print(v3)
['fdf', 'fd', '', 'dgsfbdhsfdgw']
>>> v4 = test.rsplit('s')
>>> print(v4)
['fdf', 'fd', '', 'dg', 'fbdh', 'fdgw']
#splitlines()只能以换行进行分割,可以设置True或False来确认是否保留换行符
>>> test1 = "dsfs\nfdfdf\n"
>>> v5 = test1.splitlines(True)
>>> print(v5)
['dsfs\n', 'fdfdf\n']
>>> v6 = test1.splitlines(False)
>>> print(v6)
['dsfs', 'fdfdf']
#大小写转换
>>> test = "dljkdfjJJLKFD"
>>> v = test.swapcase()
>>> print(v)
DLJKDFJjjlkfd
#替换,可以接收三个参数,前两个为替换内容,最后一个为替换次数
>>> test = "tronytronytrony"
>>> v = test.replace("tr", "bbb", 3)
>>> print(v)
bbbonybbbonybbbony























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值