(1)len(str)计算字符串的总数
(2)find()与rfind()查找字符所在的索引
(3)index()与rindex()查找字符所在的索引
# 字符串的计算方法:
# len(str)计算字符串的字符个数
n = "abcdefn"
print("字符串的总长度:",len(n))
find(sub,start,end)查找字符索引参数(从左到右)
参数1-sub 需要检索的字符串
参数2-start 检索的起始位置 可省略,默认0
参数3-end 检索的结束位置 可省略,默认len(str)
print("find字符‘c’的下标:",n.find("c"))
rfind功能使用,同find,区别:从右往左进行查找
print("rfind字符‘c’的下标:",n.rfind("c"))
index(sub,start=0,end=len(str))查找字符索引参数(从左到右)
参数1-sub 需要检索的字符串
参数2-start 检索的起始位置可省略,默认0
参数3-end 检索的结束位置可省略,默认len(str)
print("index字符‘c’的下标:",n.index("c"))
rindex功能使用,同index,区别:从右往左进行查找
print("index字符‘c’的下标:",n.rindex("c"))
count(sub,end=len(str))计算某个子字符串的出现个数
参数1-sub 需要检索的字符串
参数2-start 检索的起始位置可省略,默认len(str)
print("count字符‘c’出现的次数:",n.count("c"))
--------------输出--------------------
字符串的总长度: 7
find字符‘c’的下标: 2
rfind字符‘c’的下标: 2
index字符‘c’的下标: 2
index字符‘c’的下标: 2
count字符‘c’出现的次数: 1
# 字符串的转换
string = "abcdef"
# replace使用给定的新字符串 替换原字符串中的 旧字符串
# replace(old,new[,count])
# 参数1-old需要被替换的旧字符串
# 参数2-new替换后的新字符串
# 参数3-count替换的个数可省略,表示替换全部
print("v代替c后的字符串:",string.replace("c","v",1))
capitalize将字符串首字母变为大写
capitalize()
参数:无
print("将字符串首字母变为大写",string.capitalize())
title将字符串每个单词的首字母变为大写
title()
参数:无
print("将字符串每个单词的首字母变为大写",string.title())
lower将字符串每个字符都变为小写
lower()
参数:无
str2 = "ABCDEF"
print("将字符串每个字符都变为小写",str2.lower())
upper将字符串每个字符都变为大写
upper()
参数:无
str3 = "abcdef"
print("将字符串每个字符都变为大写",str3.upper())
---------------输出-------------------
v代替c后的字符串: abvdef
将字符串首字母变为大写 Abcdef
将字符串每个单词的首字母变为大写 Abcdef
将字符串每个字符都变为小写 abcdef
将字符串每个字符都变为大写 ABCDEF
# 字符的填充与压缩
str1 = "abc"
# ljust根据指定字符(1个),将原字符串填充够指定长度
# l 表示原字符串靠左
# 语法ljust(width,fillchar)
# 参数1-width 指定结果字符串的长度
# 参数2-fillchar 如果原字符串长度 < 指定长度时 填充过去的字符
print("字符串靠L 填充X后",str1.ljust(8,"x"))
rjust根据指定字符(1个),将原字符串填充够指定长度
r 表示原字符串靠右
语法rjust(width,fillchar)
参数1-width 指定结果字符串的长度
参数2-fillchar 如果原字符串长度 < 指定长度时 填充过去的字符
print("字符串靠R 填充X后",str1.rjust(8,"x"))
center根据指定字符(1个),将原字符串填充够指定长度
c 表示原字符串靠右
语法center(width,fillchar)
参数1-width 指定结果字符串的长度
参数2-fillchar 如果原字符串长度 < 指定长度时 填充过去的字符
print("字符串靠C 填充X后",str1.center(8,"x"))
lstrip移除所有原字符串指定字符(默认为空白字符)
l 表示从左侧开始移除
语法 lstrip(chars)
参数-chars 需要移除的字符集
str2 = "wwwwwdfddfdddrttttt"
print("把d删除后的字符串",str2.lstrip("w"))
rstrip移除所有原字符串指定字符(默认为空白字符)
r 表示从左侧开始移除
语法 lstrip(chars)
参数-chars 需要移除的字符集
str2 = "wwwwwdfddfdddrttttt"
print("把右边的t删除后的字符串",str2.rstrip("t"))
--------------输出------------------
字符串靠L 填充X后 abcxxxxx
字符串靠R 填充X后 xxxxxabc
字符串靠C 填充X后 xxabcxxx
把d删除后的字符串 dfddfdddrttttt
把右边的t删除后的字符串 wwwwwdfddfdddr
<h1 id="四字符串的分割拼接">(四)字符串的分割拼接
# 字符串的分割
# split
# 作用 将一个大的字符串分割成几个子字符串
# 语法 split(sep,maxsplit)
# 参数1-sep 分隔符
# 参数2-maxsplit 最大的分割次数 可省略,有多少分割多少
# 返回值 分割后的子字符串,组成的列表 list 列表类型
str1 = "df-fdfgdf-12345-"
print("split",str1.split("-",2))
partition
作用 根据指定的分隔符,返回(分隔符左侧内容,分隔符,分隔符右侧内容)
语法 partition(sep)
参数-sep 分隔符
str2 = "zsj-2038145339"
print("partition:",str2.partition("-"))
splitlines 按照换行符(r,n),将字符串拆成多个元素,保存到列表中
语法 splitlines(keepends)
参数-keepends 是否保留换行符 bool 类型
str3 = "jkdjfkdn123456"
print("splitlines:",str3.splitlines()) # 默认为False
print("splitlines:",str3.splitlines(True)) # 是 保留换行符
join 根据指定字符串,将给定的可迭代对象,进行拼接,得到拼接后的字符串
语法 join(iterable)
参数 iterable 可迭代的对象 字符串 元组 列表 ...
items = ["zyg","机械","175"]
str4 = "-".join(items)
print("join:",str4)
-----------------输出--------------------
split ['df','fdfgdf','12345-']
partition: ('zsj','-','2038145339')
splitlines: ['jkdjfkd','123456']
splitlines: ['jkdjfkdn','123456']
join: zyg-机械-175
<h1 id="五字符串的判定">(五)字符串的判定
# 字符的判定
# isalpha 字符串中是否所有的字符都是字母 不包含该数字,特殊符号,标点符号等等
# 语法 isalpha()
# 参数 无
str1 = "apple"
str2 = "apple123"
print("isalpha:apple",str1.isalpha())
print("isalpha:apple123",str2.isalpha())
isdigit 字符串中是否所有的字符都是数字 不包含该字母,标点符号等等
语法 isdigit()
参数 无
str3 = "123456"
print("isdigit: apple123",str2.isdigit())
print("isdigit: 123456",str3.isdigit())
isalnum 字符串中是否所有的字符都是数字或者字母 不包含该特殊符号,标点符号等等
语法 isalnum()
参数 无
str4 = "+++--&&&"
print("isalnum: apple123",str2.isalnum() )
print("isalnum: +++--&&&",str4.isalnum() )
isspace 字符串中是否所有的字符都是空白符 包括空格,缩进,换行等不可见转义符
语法 isspace()
参数 无
str5 = "nntt "
print(r"isspace: nntt",str5.isspace())
print("isspace: +++--***&&&",str4.isspace())
startswith 判定一个字符串是否以某个前缀开头
语法 startswith(prefix,end=len(str))
参数1-prefix 需要判定的前缀字符串
参数2-start 判定起始位置
参数3-end 判定结束位置
print("startswith :a",str1.startswith("a",-1))
print("startswith :x",str1.startswith("x",-1))
endswith 判定一个字符串是否以某个后缀结尾
语法 endswith(suffix,end=len(str))
参数1-suffix 需要判定的后缀字符串
参数2-start 判定起始位置
参数3-end 判定结束位置
print("endswith: e",str1.endswith("e",-1))
print("endswith: x",str1.endswith("x",-1))
in 判定一个字符串,是否被另外一个字符串包含
not in 判定一个字符串,是否不被另外一个字符串包含
print("i是否在'string'","i" in "string")
print("i是否不在'string'","i" not in "string")
--------------输出------------------
isalpha:apple True
isalpha:apple123 False
isdigit: apple123 False
isdigit: 123456 True
isalnum: apple123 True
isalnum: +++--&&& False
isspace: nntt True
isspace: +++--&&& False
startswith :a True
startswith :x False
endswith: e False
endswith: x False
i是否在'string' True
i是否不在'string' False
<h3 id="本小节结束">本小节结束!
我是张一根,一个机械专业的大学生,一个在读本科的计算机爱好者,期盼和你一起交流计算机的知识,让我们在这个大时代里一起进步。
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|