4.4Python数据类型(4)之字符串函数

返回总目录

目录:

1.字符串的查找计算

2.字符串的转换

3.字符串的填充压缩

4.字符串的分割拼接

5.字符串的判定

(一)字符串的查找计算

(1)len(str)计算字符串的总数
(2)find()与rfind()查找字符所在的索引
(3)index()与rindex()查找字符所在的索引

# 字符串的计算方法:
# len(str)计算字符串的字符个数
n = "abcdef
"
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, start=0, end=len(str))计算某个子字符串的出现个数
# 参数1-sub 需要检索的字符串
# 参数2-start  检索的起始位置可省略, 默认0
# 参数3-end	检索的结束位置可省略, 默认len(str)
print("count字符‘c’出现的次数:", n.count("c"))

--------------输出--------------------
字符串的总长度: 7
find字符‘c’的下标: 2
rfind字符‘c’的下标: 2
index字符‘c’的下标: 2
index字符‘c’的下标: 2
count字符‘c’出现的次数: 1

注意:find与index的唯一区别在于在于他们找不到对应的字符的索引时报错的情况不一样。

(1)find()与rfind()找不到时,返回-1

(2)index()与rindex()找不到时,异常错误

(二)字符串的转换

# 字符串的转换
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

注意:源字符串并没有改变,只是返回改变后的字符。

还有三个与上述方法对应的判断方法:

(1)istitle

(2)islower

(3)isupper

(三)字符串的填充压缩

# 字符的填充与压缩
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

(四)字符串的分割拼接

# 字符串的分割
# 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 按照换行符(
, 
), 将字符串拆成多个元素, 保存到列表中
# 	语法 splitlines(keepends)
#   参数-keepends 是否保留换行符 bool 类型
str3 = "jkdjfkd
123456"
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: ['jkdjfkd
', '123456']
join: zyg-机械-175

(五)字符串的判定

# 字符的判定
# 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 = "

		 "
print(r"isspace: 

		", str5.isspace())
print("isspace: +++--***&&&", str4.isspace())

# startswith 判定一个字符串是否以某个前缀开头
# 	语法 startswith(prefix, start=0, end=len(str))
#   参数1-prefix 需要判定的前缀字符串
# 	参数2-start 判定起始位置
# 	参数3-end 判定结束位置
print("startswith :a", str1.startswith("a", 0, -1))
print("startswith :x", str1.startswith("x", 0, -1))

# endswith 判定一个字符串是否以某个后缀结尾
# 	语法 endswith(suffix, start=0, end=len(str))
#   参数1-suffix 需要判定的后缀字符串
# 	参数2-start 判定起始位置
# 	参数3-end 判定结束位置
print("endswith: e", str1.endswith("e", 0, -1))
print("endswith: x", str1.endswith("x", 0, -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: 

		 True
isspace: +++--***&&& False
startswith :a True
startswith :x False
endswith: e False
endswith: x False
i是否在'string' True
i是否不在'string' False

本小节结束!

返回总目录

我是张一根,一个机械专业的大学生,一个在读本科的计算机爱好者,期盼和你一起交流计算机的知识,让我们在这个大时代里一起进步。

原文地址:https://www.cnblogs.com/zyg123/p/10178474.html