pythonday4字符串,列表相关方法

python

# ### 字符串的相关方法
# *capitalize 字符串首字母大写
strvar = "i am a boy"
# strvar = "我是一个男孩ooo" # 对于中文特殊字符无效
res = strvar.capitalize()
print(res)

# *title 每个单词的首字母大写 (非字母隔开的单词)
strvar = "you are beautiful"
res = strvar.title()
print(res)

# *upper 将所有字母变成大写
strvar = "you are beautiful"
res = strvar.upper()
print(res)
# *lower 将所有字母变成小写
res = strvar.lower()
print(res)

# *swapcase 大小写互换
strvar = "You Are Beautiful"
res = strvar.swapcase()
print(res)

# *count 统计字符串中某个元素的数量
strvar = "我好喜欢你哦哦哦哦哦"
res = strvar.count("哦")
# strvar.count(val,start,end) # end最大值取不到,取到它之前的元素
res = strvar.count("哦",1,5) # 1 2 3 4
print(res)

# *find 查找某个字符串第一次出现的索引位置 (推荐)
"""strvar.find(val,start,end) """
strvar= "oh Father this is my favorate girl"
res = strvar.find("Father") # 3
# 区分大小写,从4这个索引下标开始寻找
res = strvar.find("f",4) # 从4开始找,一直到最后 => 21
# 当找不到对应字符的时候,返回的是-1
res = strvar.find("f",4,20) # 4~19 => -1
print(res)


# *index 与 find 功能相同 find找不到返回-1,index找不到数据直接报错 (了解)
res = strvar.index("f",4)
# res = strvar.index("f",4,20) 直接报错
print(res)

# *startswith 判断是否以某个字符或字符串为开头
"""strvar.startswith(val,start,end) """
strvar= "oh Father this is my favorate girl"
res = strvar.startswith("oh")
res = strvar.startswith("this",10)
res = strvar.startswith("this",10,14) # 10 11 12 13
print(res)

# *endswith 判断是否以某个字符或字符串结尾
res = strvar.endswith("girl")
res = strvar.endswith("rate",-9,-5) # -9 -8 -7 -6
print(res)


# 重要 ***
# *split 按某字符将字符串分割成列表(默认字符是空格)
strvar = "you can you up no can no bb"
lst = strvar.split()
strvar = "you@can@you@up@no@can@no@bb"
lst = strvar.split("@")
# 第二个参数值=>切割的次数
lst = strvar.split("@",2)
print(lst)

# *join 按某字符将列表拼接成字符串(容器类型都可)
lst = ["you", "can", "you", "up", "no", "can", "no", "bb"]
strvar = " ".join(lst)
print(strvar)


# *replace 替换字符串(可选择替换的次数)
"""replace("被替换的字符串","要替换的字符串","替换的次数")"""
strvar = "大风车呀,转呀转呀转呀,今天的节目真好看"
strvar1 = strvar.replace("转呀","跑呀")
strvar2 = strvar.replace("转呀","跑呀",1)
print(strvar2)


# *isdecimal 检测字符串是否以数字组成 必须是纯数字
strvar = "13424sasdf"
strvar = "1342423423423"
res = strvar.isdecimal()
print(res)

#isspace 判断字符串是否由空白符组成
strvar = "

"
res = strvar.isspace()
print(res)


strvar = "我爱你,你爱我么?我爱你个锤子"
res = len(strvar)
print(res)


# *center 填充字符串,原字符居中 (默认填充空格)
strvar = "刘德华"
# 10代表的是总长度 = 填充符号的长度 + 原字符串的长度
res = strvar.center(10) # 默认填充空格
res = strvar.center(10,"@")
print(res)

# *strip 默认去掉首尾两边的空白符
"""场景:在数据存储时,要先把两边的空白符去掉在存储."""
print("<==============>")
strvar = " 刘德华 "
print(strvar)
res = strvar.strip()
print(res)

strvar = " @@@刘德华###"
res = strvar.strip(" @#")
print(res,"<----->")


# rstrip 去掉右边的空白符
strvar = " 刘德华 "
strvar1 = strvar.rstrip()
print(strvar1)
# lstrip 去掉左边的空白符
strvar = " 刘德华 "
strvar2 = strvar.lstrip()
print(strvar2)

出处:https://www.cnblogs.com/liucx

# ### 列表的相关方法
# (1)列表的拼接 (同元组) *
lst1 = ["小小","小强","小康"]
lst2 = ["小刘","小黄","小辉"]
lst = lst1 + lst2
print(lst)

# (2)列表的重复 (同元组) *
lst1 = ["小小","小强","小康"]
lst = lst1 * 3
print(lst)

# (3)列表的切片 (同元组)
# 语法 => 列表[::] 完整格式:[开始索引:结束索引:间隔值]

lst = ["铁拐李","张果老","吕公滨","何仙姑","蓝采和","曹国舅","小强","汉钟离"]
# (1)[开始索引:] 从开始索引截取到列表的最后
res = lst[3:]
print(res)
# (2)[:结束索引] 从开头截取到结束索引之前(结束索引-1)
res = lst[:6] # 0 1 2 3 4 5
print(res)

# (3)[开始索引:结束索引] 从开始索引截取到结束索引之前(结束索引-1)
res = lst[-3:-1] # -3 -2
print(res)

# (4)[开始索引:结束索引:间隔值] 从开始索引截取到结束索引之前按照指定的间隔截取列表元素值
# 从右向左截取
res = lst[-2:-7:-1] #-2 -3 -4 -5 -6
print(res)

# 从左向右截取
res = lst[-4:-1:2] #-4 -2
print(res)

# (5)[:]或[::] 截取所有列表
res = lst[:]
res = lst[::]
print(res)


# (4)列表的获取 (同元组)
# 0 1 2
lst = ["小小","小强","小康"]
# -3 -2 -1
res = lst[-1]
res = lst[2]
print(res)


# (5)列表的修改 ( 可切片 )
# 一次改一个
lst = ["小小","小强","小康"]
lst[0] = "小龙"
print(lst)

# 一次改一堆
"""切片赋值时,必须是可迭代性的数据(容器类型数据,range对象,迭代器) """
lst = ["铁拐李","张果老","吕公滨","何仙姑","蓝采和","曹国舅","小强","汉钟离"]
# 在用切片修改值的时候,先把值找到,然后删掉,把替换的值一个个的放到被切出的元素位置.
lst[2:5] = [1,2,3,4] # 2 3 4
lst[2:6] = "abcd"
print(lst)

# 如何切片时,使用了步长,那么切多少,改多少
lst = ["铁拐李","张果老","吕公滨","何仙姑","蓝采和","曹国舅","小强","汉钟离"]
lst[::3] = ("小亚","小玮","小溪") # 0 3 6 铁拐李 何仙姑 小强
print(lst)

# (6)列表的删除 ( 可切片 )
lst = ["铁拐李","张果老","吕公滨","何仙姑","蓝采和","曹国舅","小强","汉钟离"]
# 删除单个
# del lst[0]
# print(lst)

# 删除多个
# del lst[2:6] # 2 3 4 5
# print(lst)
res = lst[0]
# 删除的是res变量和列表无关.
del res
print(lst)

以上是 pythonday4字符串,列表相关方法 的全部内容, 来源链接: utcz.com/z/530264.html

回到顶部