广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python 字符串常用方法超详细梳理总结
  • 345
分享到

python 字符串常用方法超详细梳理总结

2024-04-02 19:04:59 345人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录字符串是什么?字符串常用方法1.find()2.index()3.startswith()4.endswith()5.count()6.join()7.upper()8.lowe

Python基础之字符串常用方法???

持续更新python基础知识,欢迎各位来访~???

字符串是什么?

字符串就是一系列字符。字符串属于不可变序列,在Python中,用引号包裹的都是字符串,其中引号可以是单引号,双引号,也可以是三引号(单,双引号中的字符必须在一行,三引号中的字符可以分布在多行)

txt = 'hello world' # 使用单引号,字符串内容必须在一行
txt1 = "hello python world " # 使用双引号,字符串内容必须在一行
# 使用三引号,字符串内容可以分布在多行
txt2 = '''life is short
i use python '''

字符串常用方法

1.find()

定义 find()方法返回该元素最小索引值(找不到返回-1) ?举个栗子?返回"python"的最小索引值


txt = "hello python world."
res = txt.find("python")
print(res)

运行结果如下:

6

2.index()

定义 index()方法返回该元素最小索引值(找不到元素会报错) ?举个栗子?返回"world"的最小索引值


txt = "hello python world."
res = txt.index("world")
print(res)

运行结果如下:

13

3.startswith()

定义 startswith() 方法如果字符串以指定值开头,返回True,否则返回False ?举个栗子?判断字符串是不是以"hello"开头


txt = "hello python world."
res = txt.startswith("hello")
print(res)

运行结果如下:

True

4.endswith()

定义 endswith() 方法如果字符串以指定值结束,返回True,否则返回False ?举个栗子?判断字符串是不是以"hello"结束


txt = "hello python world."
res = txt.endswith("hello")
print(res)

运行结果如下:

Flase

5.count()

定义 count() 方法返回指定值在字符串中出现的次数。 ?举个栗子?统计"o"出现次数


txt = "hello python world."
res = txt.count("o")
print(res)

运行结果如下:

3

6.join()

定义 join() 方法获取可迭代对象中的所有项目,并将它们连接为一个字符串。必须将字符串指定为分隔符 ?举个栗子?使用"-"作为分割符,将列表中的所有项连接到字符串中


res = ['h','e','l','l','o']
print('-'.join(res))

运行结果如下:

h-e-l-l-o

7.upper()

定义 upper()方法将字符串全部转为大写 ?举个栗子?将字符串"hello python world"全部转为大写


tet = "hello python world"
res = txt.upper()
print(res)

运行结果如下:

HELLO WORLD

8.lower()

定义 lower()方法将字符串全部转为小写 ?举个栗子?将字符串"HELLO PYTHON WORLD"全部转为小写


tet = "HELLO PYTHON WORLD"
res = txt.lower()
print(res)

运行结果如下:

hello python world

9.split()

定义 split()方法以指定字符分割字符串,并返回列表 ?举个栗子?以?号作为分隔符,分割字符串


txt = "hello?python?world"
res = txt.split("?")
print(res)

运行结果如下:

['hello', 'python', 'world']

?扩展?分割后打印还是原字符串(字符串是不可变类型,分割操作是复制一份原字符串,更改的是复制出来的那一份)


txt = "hello?python?world"
res = txt.split("?")
# 打印分割后的
print(res)
# 打印原字符串
print(txt)

['hello', 'python', 'world']
hello?python?world

10.strip()

定义 strip()方法删除字符串两端的空格 ?举个栗子?删除hello两端的空格


txt = "    hello  "
res = txt.strip()
print(res)

运行结果如下:

hello

11.replace()

定义 replace()方法以指定内容替换掉被指定内容(默认替换全部,可指定替换次数) ?举个栗子?以java替换python


txt = 'hello python world'
res = txt.replace('python','java')
print(res)

运行结果如下:

hello java world

?扩展?替换后打印还是原字符串(字符串是不可变类型,替换操作是复制一份原字符串,更改的是复制出来的那一份)


txt = 'hello python world'
res = txt.replace('python','java')
# 打印替换后的
print(res)
# 打印原字符串
print(txt)

hello java world
hello python world

以上就是字符串常用的方法整理

到此这篇关于python 字符串常用方法超详细梳理总结的文章就介绍到这了,更多相关python 字符串方法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: python 字符串常用方法超详细梳理总结

本文链接: https://www.lsjlt.com/news/142878.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作