栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 字符串常用方法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 字符串常用方法

常见字符串类型方法
    • 1、find 方法
    • 2、count 方法
    • 3、replace 方法
    • 4、spilt 方法
    • 5、startswith 方法
    • 6、endswith 方法
    • 7、lower 方法
    • 8、upper 方法
    • 9、join 方法
    • 10、字符串翻转

1、find 方法

(1)参数说明及用法

find(sub, start=None, end=None)
# sub:查找的字符串
# start:起始索引
# end:结束索引

返回值为查找字符串的索引

(2)在索引start和end之间查找字符串sub​找到,则返回最左端的索引值,未找到,则返回-1

source_str = "There is a string accessing example"
print(source_str.find('T', 2, 10))
# 输出结果
-1

(3)start和end都可省略,省略start说明从字符串开头找省略end说明查找到字符串结尾,全部省略则查找全部字符串

source_str = "There is a string accessing example"
print(source_str.find('T'))
# 输出结果
0
2、count 方法

(1)参数说明及用法

count(sub, start=None, end=None)
# sub:查找的字符串
# start:起始索引
# end:结束索引

返回值为查找字符串的个数

(2)返回字符串sub在start和end之间出现的次数

source_str = "There is a string accessing example"
print(source_str.count('e'))
print(source_str.count('e', 0, 4))

#输出结果
5
1
3、replace 方法

(1)参数说明及用法

replace(old, new, count=None)

# old代表需要替换的字符,new代表将要替代的字符,count代表替换的次数(省略则表示全部替换)

(2)效果展示

source_str = "There is a string accessing example"
print(source_str.replace('e', 'E'))
print(source_str.replace('e', 'E', 1))

# 结果展示
ThErE is a string accEssing ExamplE
ThEre is a string accessing example
4、spilt 方法

(1)参数说明及用法

split(sep, maxsplit=None)

# 以sep为分隔符切片,如果maxsplit有指定值,则仅分割maxsplit个字符串分割后原来的str类型将转换成list类型

(2)结果展示

source_str = "There is a string accessing example"
print(source_str.split('e'))
print(source_str.split('e', maxsplit=3))

# 输出结果
['Th', 'r', ' is a string acc', 'ssing ', 'xampl', '']
['Th', 'r', ' is a string acc', 'ssing example']
5、startswith 方法

(1)参数说明及用法

startswith(prefix, start=None, end=None)

# 判断字符串是否是以prefix开头,start和end代表从哪个下标开始,哪个下标结束

(2)结果展示

source_str = "There is a string accessing example"
print(source_str.startswith('T', 0, 10))
print(source_str.startswith('T'))

# 输出结果
True
True
6、endswith 方法

(1)参数说明及用法

endswith(suffix, start=None, end=None)

# 判断字符串是否以suffix结束,如果是返回True,否则返回False

(2)结果展示

source_str = "There is a string accessing example"
print(source_str.endswith('e'))
print(source_str.endswith('e', 0, 10))

# 输出结果
True
False
7、lower 方法

(1)参数说明及用法

source_str.lower()

(2)结果展示

source_str = "There is a string accessing example"
print(source_str.lower())
# 将所有字符串变小写

结果:
there is a string accessing example
8、upper 方法

(1)参数说明及用法

source_str.upper()

(2)结果展示

source_str = "There is a string accessing example"
print(source_str.upper())
# 将所有字符串变小写

结果:
THERE IS A STRING ACCESSING EXAMPLE
9、join 方法

(1)使用方法

# 将列表对象拼接成字符串

(2)结果展示

data = ['a', 'b', 'c']
print(''.join(data))

结果展示
abc
10、字符串翻转
source_str = "There is a string accessing example"
print(source_str[::-1])

结果展示
elpmaxe gnissecca gnirts a si erehT
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/958273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号