Python字符串

Python字符串

string 是用于保存字符信息的容器:

    创建:str1= "xxxx "   字符串可使用转义字符   \n   \"  \t 等

            str1 ='xxxx '

            str1 = ''' xxxx'''

            str1 = """xxxx"""      可以回车

 

字符串基本操作

公共函数

功能

容器[索引]

获取容器对应的位置数据

容器.index(数据)

获取首次出现的索引

index(数据,star , end)

在指定范围首次出现

count(数据)

获取数据在容器的数量

len

求长度

in

是否包含

not in

是否不包含

max(容器)

求最大值

min(容器)

求最小值

排序权重

空格<符号<数字<字母<汉字

 

字符串判断型操作

    特征: 方法名以 is 开头,后面跟上要判断的类别

    返回值为 bool

 

    .islower() 纯小写

    .isupper() 纯大写

 

    .isdigit() 判断数字

    .isalpha() 是否全字母

    .isalnum() 是否是字母数字

    .istitle() 是否单词首字母大写

    .startswith(str)  是否以指定字符串开始

    .endswith(str) 是否以指定字符串结束

 

数据转换型

    lower() 字符串中所有可转换字符转换成小写字符

    casefold()  兼容更多文字类别

    upper() 转大写字符

    swapcase() 大转小 小转大

    title()     单词首字母大写其他字符转小写字符

    capitalize() 字符串首字母大写,其余字母小写

 

格式转换型

    strip(占位符)    去字符串左右两侧占位符   省略默认位空格 空白

    lstrip(占位符)    去字符串左侧占位符

 

    ljust(长度,占位符) 左占位右侧补占位符

    rjust(长度,占位符) 右占位左侧补占位符

    center(长度,占位符) 补两侧 如果是单数,左多一

    zfill(长度)         左侧补0

 

拆分与连接

    partition(字符串)    左侧切割成3组数据的元组(参数左侧,参数,参数右侧)

    rpartition(字符串)   从右侧开始查找 切割成3组数据的元组(参数左侧,参数,参数右侧)

    split(字符串)  使用参数将原始字符串拆分为若干个字符串组成列表返回 (不含字符串)

    splitlines() 使用换行符(\n)作为分割线将原始字符串拆分成若干个字符串(返回列表)

    join(字符串) 将原始字符串填充到参数的每个字符之间组成新的字符串返回

    str + str  

"""
字符串的操作
字符串常用操作_判断类型
"""
str1 = "hello world!"
print(str1[1])
print(str1.index("o"))
print(str1.index("o", 5, 12))
print(str1.count("o"))
print(len(str1))
print("o" in str1)
print("o" not in str1)
print(max(str1))
print(min(str1))
# 判断型操作
str1 = "AAA"
print(str1.isupper())
print(str1.lower())  # 转换为小写
print(str1.islower())
print(str1.isprintable())
print(str1.isidentifier())
print(str1.isalpha())  # 纯字母
print(str1.isalnum())  # 字母或数据
print(str1.isnumeric())
print(str1.isdigit())
print(str1.isspace())
print(str1.istitle())
print(str1.startswith("A"))
print(str1.endswith("A"))
# 判断一个文件名是否是图片格式
file = "1.jpg"
if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".gif"):
    print(file, "是图片")

# 循环方法
jpg = [".jpg", ".png", ".gif", "jpge"]
file1 = "2.gif"
for _ in jpg:
    if file1.endswith(_):
        print(file1, "是图片")
        break
else:
    print(file1, "不是图片")

查询与替换 

    find(字符串,开始索引,结束索引)      查找?    -1

    rfind(字符串,开始索引,结束索引)      右侧查找     报错

    index(字符串,开始索引,结束索引)  从左侧开始查找字符串第一次出现的位置 (会报错)

    rindex(字符串,开始索引,结束索引)  从右侧开始查找字符串第一次出现的位置 (会报错)

    count(字符串)  查询指定字符在原始字符串中出现的次数

 

替换

    replace(old_str,new_str,num)  使用新字符串替换原始字符串

    expandtabs()  使用空格替换原始字符串中的制表位 \t   

 

字符串其他操作(生成字典与关系转换)

    加密,解密

        maketrans(字符串1,字符串2)        生成转化策略

        translate(替换策略)        执行砖和策略

 

字符串切片

    字符串[-1]        求最后一个字符

    字符串[开始索引:结束索引]

    字符串[开始索引:结束索引:步长]

"""
字符串常用操作
拆分与拼接
查询与替换
"""
str1 = "hello world \n oh!"
print(str1.partition("o"))
print(str1.rpartition("o"))
print(str1.split("o"))
print(str1.split("o", 2))
print(str1.splitlines())
str1 = "*"
str2 = "123456!"
print(str1.join(str2))
print(str1 + str2)
# 查询与替换
str1 = "hello i like python"
print(str1.find("i"))
print(str1.rfind("i"))
print(str1.index("l"))
print(str1.rindex("l"))
print(str1.replace("l", "*"))
str1 = "hello \t world \t python!"
print(str1.expandtabs())

"""
字符串常用操作
加密,解密
"""
dict1 = "".maketrans("abcdefgh", "12345678")
str1 = "cad good bay"
str2 = str1.translate(dict1)
print(str2)

dict1 = "".maketrans("12345678", "abcdefgh")
str1 = "314 7oo4 21y"
str2 = str1.translate(dict1)
print(str2)

 

 

exit(?)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值