python 字符串子串_Python字符串子字符串
python 字符串子串
A substring is the part of a string. Python string provides various methods to create a substring, check if it contains a substring, index of substring etc. In this tutorial, we will look into various operations related to substrings.
子字符串是字符串的一部分。 Python字符串提供了各种方法来创建子字符串,检查其是否包含子字符串,子字符串的索引等。在本教程中,我们将研究与子字符串相关的各种操作。
Python字符串子字符串 (Python String Substring)
Let’s first look at two different ways to create a substring.
首先让我们看一下创建子字符串的两种不同方法。
创建一个子串 (Create a Substring)
We can create a substring using string slicing. We can use split() function to create a list of substrings based on specified delimiter.
我们可以使用字符串切片来创建子字符串 。 我们可以使用split()函数根据指定的分隔符创建子字符串列表。
s = 'My Name is Pankaj'# create substring using slice
name = s[11:]
print(name)# list of substrings using split
l1 = s.split()
print(l1)
Output:
输出:
Pankaj
['My', 'Name', 'is', 'Pankaj']
检查是否找到子字符串 (Checking if substring is found)
We can use in operator or find() function to check if substring is present in the string or not.
我们可以使用in运算符或find()函数来检查字符串中是否存在子字符串。
s = 'My Name is Pankaj'if 'Name' in s:print('Substring found')if s.find('Name') != -1:print('Substring found')
Note that find() function returns the index position of the substring if it’s found, otherwise it returns -1.
注意,find()函数返回子字符串的索引位置(如果找到),否则返回-1。
子字符串出现次数 (Count of Substring Occurrence)
We can use count() function to find the number of occurrences of a substring in the string.
我们可以使用count()函数查找字符串中子字符串的出现次数。
s = 'My Name is Pankaj'print('Substring count =', s.count('a'))s = 'This Is The Best Theorem'
print('Substring count =', s.count('Th'))
Output:
输出:
Substring count = 3
Substring count = 3
查找子字符串的所有索引 (Find all indexes of substring)
There is no built-in function to get the list of all the indexes for the substring. However, we can easily define one using find() function.
没有内置函数来获取子字符串的所有索引的列表。 但是,我们可以使用find()函数轻松定义一个。
def find_all_indexes(input_str, substring):l2 = []length = len(input_str)index = 0while index < length:i = input_str.find(substring, index)if i == -1:return l2l2.append(i)index = i + 1return l2s = 'This Is The Best Theorem'
print(find_all_indexes(s, 'Th'))
Output: [0, 8, 17]
输出: [0, 8, 17]
翻译自: https://www.journaldev.com/23774/python-string-substring
python 字符串子串
python 字符串子串_Python字符串子字符串相关推荐
- python截取子串_python字符串截取子串
在python中没有类似sub()或者subString()的方法,但是字符串的截取操作却是更加简单. 只需要把字符串看作是一个字符数组,截取子串非常方便. 多余的话就不啰嗦了,看下面的例子就明白了. ...
- python字符串解释_Python学习:字符串的简单解释,深入浅出
字符串是python很常见的一种数据类型,比如日志的打印,程序中函数的注释,数据库的访问,变量的操作都需要用到字符串. 一.字符串基础 字符串是由独立字符组成的一个序列,通常包含在单引号('')双引号 ...
- python核心数据类型_Python核心数据类型——字符串
字符串 Python的字符串是一个有序的字符集合,有序指的是可以通过偏移来访问每个字符,每个字符有严格的从左到右的位置顺序,类似于数组.Python中没有单个字符的类型(C语言中的char),取而代之 ...
- python字符串输入_python如何输入字符串
字符串是 Python 中最常用的数据类型.我们可以使用引号( ' 或 " )来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.例如:var1 = 'Hello World!' v ...
- python字符串解释_python基础之字符串详解
1.Python环境配置: 下载好之后安装,注意勾选环境变量. 2.写python一定要注意代码的缩进. 2.字符串: (1).字符串是从左到右是从0开始索引,从右到左是从-1开始.遵循包前不包后原则 ...
- python字符串定义符_python入门——定义字符串
坚持每天更新,帮助入门python. kali linux 小伙伴们,大家好,今下午我们一起学习在python中定义字符串. 那么什么是字符串呢?字符串或串(String)是由数字.字母.下划线组成的 ...
- python定义字符串类型_Python数据类型之字符串
1. Python字符串的创建 字符串是Python中最常见的数据类型,通常使用单引号或双引号来定义一个字符串,如下: str = "我是字符串" str1 = '我也是字符串' ...
- python如何判断字符串长度_Python如何查找字符串的长度?(代码示例)
在Python中字符串是Unicode代码点的不可变序列.给定一个字符串,我们如何查找它的长度?本篇文章就来给大家介绍在Python中查找字符串长度的四种方法,希望对大家有所帮助.[相关视频教程推荐: ...
- python中如何截取字符串函数_python中如何截取字符串函数_python截取指定字符串_Python 字符串操作(string...
在python有各种各样的string操作函数.在历史上string类在python中经历了一段轮回的历史.在最开始的时候,python有一个专门的string的module,要使用string的方法 ...
最新文章
- 输出内容时后面显示乱码
- redis 附近的人_Redis GEO地理位置信息,查看附近的人
- oracle的隐式游标有哪些,Oracle隐式游标小例子
- Linux下网络socket编程——实现服务器(select)与多个客户端通信
- STM32 串行通信原理
- 如何解决多线程并发访问一个资源的安全性问题?
- testNG单元测试学习
- android 软键盘 状态,Android监听软键盘状态
- 【转】CentOS系统操作下安装相关各种软件
- 江苏省政府投资基金集聚区落地,基金目标规模再增1500亿
- Windows 上如何制作简谱
- 【Red5流媒体服务器搭建】
- linux打补丁教程,Linux下patch打补丁命令
- vc830l 说明书_有了解vc830l万用表使用方法的吗?
- 图书管理系统课程设计
- python开方 运算符_[转载] Python中的算数运算符
- 矩阵合同,相似与等价 以及初等变换矩阵
- 水库水雨情监测系统方案分享-水库水位监测-水情监测
- 【解决】移动用户如何使用APP自行取消全国亲情网业务
- c#笔试基础(转载)
热门文章
- android之wifi开发
- webservice wsdl 生成服务
- selenium webdriver 学习总结-元素定位
- 循环渐进NsDoor(三)
- Python程序提示出现File stdin,line 1错误解决方法
- [转载] python列表解释(list comprehension)记录
- [转载] Python字符串:大写 str.upper()
- [转载] python模块的分类有哪些_整理了一份清单,常见Python问题的快速解答包
- Vue.js 学习笔记 一
- 11 旋转数组的最小数字