python第五天(字符串)

字符串

###########################6个基本技能########################
# join
# split
# find
# strip
# upper
# lower
# replace

首字母大写

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

大写转换为小写

test = "aLex"
# casrfold 可以将所有格式转换为小写
v1 = test.casefold()
print(v1)
# lower 只能将普通大写转换为小写
v2 = test.lower()
print(v2)

居中并在两端加字符串

test = "alex"
#前面加宽度,并将内容居中
v = test.center(20,"*")
print(v)

字符串内容计数

计算字符串中出现多少次

test = "alexalexalex"
# 计算字符串中关键字出现多少
# 其中3代表从第几个位置往后找,可以设置起始位置和结束位置
v = test.count("e",3)
print(v)

判断以什么结尾,或以什么开始

test = "alexalexalexalex"
# 判断以什么结尾,
v = test.endswith("x")
# 判断以什么开始
v1 = test.startswith("a")
print(v)
print(v1)

获取查找内容的位置

test = "alexalexalexalex"
# 获取查找内容的位置,可以加起始位置和结束位置
v = test.find("ex",5,8)
print(v)

index找不到会报错

test = "alexalexalexalex"
v = test.index("e")
print(v)

 

对字符串进行格式化,可以默认后面替换内容

test = "i am {name}, age {a}"
# 格式化将字符串中的占位符,替换为指定的值
v = test.format(name="alex",a = 19)
print(v)
test = "i am {0}, age {1}"
# 格式化将字符串中的占位符,替换为指定的值
v = test.format("alex",19)
print(v)
test = "i am {name}, age {a}"
#另一种字典的用法
v = test.format_map({"name":"alex","a":19}) print(v)

判断字符串是否只包含字母和数字

test ="uasf890_+"
# 判断字符串是否只包含字母和数字
v = test.isalnum()
print(v)

制表设定组值

s = "username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123"
# 每一个不足的位数加空值,然后根据换行和制表位进行制作表格
# expandtabs断句20个一组
a = s.expandtabs(20)
print(a)

判断是否仅为字母或汉字

test = "af2sa"
# 判断是否仅为字母
v = test.isalpha()
print(v)

判断当前是否为数字

test = "123"
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1,v2)
test1 = "a123"
v11 = test1.isdecimal()
v22= test1.isdigit()
print(v11,v22)
test2 = ""
v111 = test2.isdecimal()
v222 = test2.isdigit()
print(v111,v222)
test3 = "二"
v1111 = test3.isdecimal()
v2222 = test3.isdigit()
v3333 = test3.isnumeric()
print(v1111,v2222,v3333)

查询字符串是否有不可见字符(\n显示true,\t显示false)

test = "osid/njosadg"
# 是否纯在不可显示的字符
v = test.isprintable()
print(v)

判断是否全部为空格

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

将首字母大写和判断是否为首字母大写

test = "Return True if the string is a title-cased string, False otherwise"
a = test
v = test.title()
print(v)
v1 = a.istitle()
print(v1)
v2 = v.istitle()
print(v2)
将内容插入每个字符串的字符中
test = "你是风儿我是沙"
print(test)
t = " ——_"
v = t.join(test)
print(v)

在左侧或右侧添加填充符

test = "alxex"
v = test.ljust(20,"*")
print(v)
v1 = test.rjust(40,"*")
print(v1)

 自动填充0

test = "alex"
v = test.zfill(20)
print(v)

判断小写字母,和变更小写字母

test = "Alex"
v1 = test.islower()
v2 = test.lower()
print(v1,v2)

判断是否是大写字母,和变更为大写字母

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

去除左右两边的空白,或者去除换行(可以去除指定内容)

test = " al ex "
v1 = test.lstrip()
v2 = test.rstrip()
v3 = test.strip()
print(v1)
print(v2)
print(v3)

创建字符串对应关系,并进行替换

test = "aeiou"
test1 = "12345"
v = "asdlfjseiowrut"
m = str.maketrans("aeiou","12345")
new_v = v.translate(m)
print(new_v)

字符串分割

# partition 可以按关键字分割成三份
test = "tsetasdfsggs"
v1 = test.partition("s")
print(v1)
v2 = test.rpartition("s")
print(v2)
# split默认全部分割,且分割字符消失,可以设置参数对分割数量进行限定
v3 = test.split("s",1)
print(v3)
v4 = test.rsplit("s",2)
print(v4)

分割换行符,并根据布尔值确定是否保存换行符

test = "dsk;gk;lsk\nafsadg\nafsg"
v = test.splitlines(True)
v1 = test.splitlines(False)
print(v)
print(v1)

是否已某个字符串开头或者结尾

test = "backend 1.1.1.1"
v = test.startswith("a")
print(v)
v1 = test.endswith("1")
print(v1)

字符串大小写转换

test = "aLex"
v = test.swapcase()
print(v)

字符串替换

test = "alexalexalexalexalex"
v = test.replace("ex","bbb")
print(v)

输入数据范围,并设置步长

v = range(0,100,5)
for itme in v:
    print(itme)

 

posted @ 2023-08-11 14:27  贫道财迷  阅读(15)  评论(0)    收藏  举报