二。字符串str
capitalize()首字母大写
test = "alex"
v = test.capitalize()
print(v)
casefold()/lower()所有字符变小写,casefold更牛逼,所有未知也可以
test = "aLex"
v1 = test.casefold()
print(v1)
v2 = test.lower()
print(v2)
center设置宽度,内容居中。其中20代表宽度,中代表填充的内容(可以为空)
test = "alex"
v1 = test.center(20,"中")
v2 = test.center(20)
print(v1)
print(v2)
count去字符串中寻找 子序列出现的次数,可以设置起始和结尾的位数
test = "alexalex"
v1 = test.count('e')
print(v1)
v2 = test.count('ex', 5, 6)
print(v2)
startswith()以什么开始,endswith()以什么结尾,输出结果为布尔值(true,flase)
text = "alex"
v1 = text.startswith('a')
v2 = text.endswith('a')
print(v1)
print(v2)
find()从开始向后找寻找第一个位置,找到后不在寻找;可以限制寻扎范围5-8包含8,未找到显示-1
test = "alex"
v1 = test.find('ex')
v2 = test.find('ex',5,8)
print(v1,v2)
format()格式化,将一个字符串中的占位符替换为指定的值;可以从前往后依次站位
text = 'i am {name},age {a}'
print(text)
v1 = text.format(name='alex', a=19)
print(v1)
text = 'i am {0},age {1}'
print(text)
v2 = text.format('alex', 19)
print(v2)
format_map()格式化,对传输值格式有所限制{“name”: ‘alex’, “a”: 19}
text = 'i am {name},age {a}'
v1 = text.format(name='df', a=10)
v2 = text.format_map({"name": 'alex', "a": 19})
print(v1,v2)
index()寻找,未找到直接报错,不建议使用
test = "alex"
v1 = test.index('e2x')
print(v1)
isalnum()判断字符串中是否只包含数字和字母,返回值为布尔型
text1 = "uas464_+"
text2 = "uas12"
v1= text1.isalnum()
v2 = text2.isalnum()
print(v1,v2)