字符串的魔法

str

test = “alex”

#首字母大写
v = test.capitalize()
print(v)

#全小写 更牛逼,很多未知的对应变小写
v1 = test.casefold()
print(v1)

#全小写 只能处理普通字符的
v2 = test.lower()
print(v2)

#设置宽度,并将内容居中
#20代指总长度
#* 空白未知填充,一个字符,可有可无
v = test.center(20,“中”)
print(v)

#填充放左边
test = “alex”
v = test.ljust(20,"*")
print(v)

#填充放右边
test = “alex”
v = test.rjust(20,"*")
print(v)

test = “alex”

#去字符串中寻找,寻找子序列出现的次数
#第二个可以指定从哪个位置开始寻找
test = “aLexalex"
v = test.count(“r”,5)
print(v)

#欠
#encode
#decode

#以什么什么结尾或者以什么什么开头
test = “alex”
v = test.endswith(“ex”)
v = test.startwith(“al”)
print(v)

expandtabs,断句6,

test = “123456789\t9”
v = test.expandtabs(6)
print(v,len(v)

#从开始往后找,找到第一个之后,获取其位置
#> 或 >=
#未找到值为-1
test = “alexalex”
v = test.find(“ex”,5,8)
print(v)

#格式化,将一个字符串中的占位符替换为指定的值
test = “i am {name}”
print(test)
v = test.format (name=”alex".a=19)
print(v)

test = " i am (0), age(1)"
print(test)
v = test.format(“alex”,19)
print(v)

test = “i am {name}, age {a}”
v = test.format(name=“df”,a=10)
v2 = test.format_map{“name”:alex:,“a”:10}

#字符串中是否只包含字母和数字
test = “uasf890”
v = test.isalnum()
print(v)

#判断是否为字母,如果是返回为ture
test = “as2df”
v = test.isalpha()
print(v)

#判断当前输入是否是数字
test = “二”
v1 = test.isdecimal()包含二进制
v2 = test.isdingit()包含特殊符,但不支持中文
v3 = test.isnumeric()支持中文
print(v1,v2,v3)

#字母,数字,下划线: 标识符 def class
a = “def”
v =a.isidentifier()
print(v)

import keyword
print(keyword.iskeyword(“def”))
test = “def”
v = test.isidentifier()
print(v)

#是否存在不可显示的字符
#\t 制表符
#\n 换行
test = “oiusra\tdfjk”
v = test.isprintable()
print(v)

#判断全部是否为空格
test = " "
v = test.isspace()
print(v)

#判断是否是标题,以及变成标题
test = “Return True if all cased characters in S are uppercase and there is”
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)

v3 = v2.istitle()
print(v3)

#将字符串中的每一个元素按照指定分隔符进行拼接
test = “是风儿我是沙”
print(test)
t = " "
v = t.join(test)
print(v)

#断是否是大小写,以及变成大小写
test = “Alex”
v1 = test.islower()
v2 = test.lower()
print(v1,v2)

v1 = test.isupper()
v2 = test.upper()
print(v1,v2)

#取出左右空白
#可以去除\t \n
test = ” alex “
v = test.lstrip(“xa”) 去除左边xa
v = test.rstrip(“xa”) 去除右边xa
v = test.strip(“xa”)
print(v)

v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)

v = test.lstrip()
v = test.rstrip()
v = test.strip()
print(v)

#对应关系替换
test = “aertt”
test1 = “12345”
v = “sdfjksahfkj;dfsjghyjks;sdfhjksh”
m = str.maketrans(”aeiou",“12345”)
new_v = v.translate(m)
print(new_v)

test = “testsdfdsa”
v = test.partition(“s”) #只能分割第一个s
print(v)

v = test.rpartition(“s")
print(v)

v = test.split(“s”,1 )找1次分割,但是匹配到的s拿不到
pint(v)
test.rsplit()
pint(v)

#正则表达式( 这俩个功能的合集)
#是否想要分割的元素

#分割,只能根据换行分割。true,false:是否保留换行
test = “sdfkjdkls\nsdklfjaklsdj\nsdlfakl;”
v = test.splitlines(True)
print(v)

#以开头,以结尾
test = “backend 1.1.1.1”
v = test.startwith(a)
print(v)
test.endswith(a)

大小写转换
test = “aLex"
v = test.swapcase()
print(v)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值