Python offers many ways to substring a string. It is often called ‘slicing’.

Python提供了许多对字符串进行子字符串化的方法。 它通常被称为“切片”。

It follows this template:

它遵循以下模板:

string[start: end: step]

Where,

哪里,

start: The starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0.

start :子字符串的起始索引。 该索引处的字符包含在子字符串中。 如果不包括start ,则假定等于0。

end: The terminating index of the substring. The character at this index is NOT included in the substring. If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default.

end :子字符串的终止索引。 该索引处的字符包括在子字符串中。 如果不包括end ,或者指定的值超过字符串的长度,则默认情况下假定它等于字符串的长度。

step: Every ‘step’ character after the current character to be included. The default value is 1. If the step value is omitted, it is assumed to equal to 1.

step :要包括的当前字符之后的每个“ step”字符。 默认值为1。如果省略步长值,则假定等于1。

模板 (Template)

string[start:end]: Get all characters from index start to end-1

string[start:end] :获取从索引startend-1的所有字符

string[:end]: Get all characters from the beginning of the string to end-1

string[:end] :获取从字符串开头到end-1的所有字符

string[start:]: Get all characters from index start to the end of the string

string[start:] :获取从索引开始到字符串结尾的所有字符

string[start:end:step]: Get all characters from start to end-1 discounting every step character

string[start:end:step] :获取从startend-1的所有字符,每字符都打折

例子 (Examples)

  • Get the first 5 characters of a string

    获取字符串的前5个字符

string = "freeCodeCamp"
print(string[0:5])

Output:

输出:

> freeC

Note: print(string[:5]) returns the same result as print(string[0:5])

注意: print(string[:5])返回与print(string[0:5])相同的结果

  • Get a substring of length 4 from the 3rd character of the string

    从字符串的第三个字符获取长度为4的子字符串

string = "freeCodeCamp"
print(string[2:6])

Output:

输出:

> eeCo

Please note that the start or end index may be a negative number. A negative index means that you start counting from the end of the string instead of the beginning (i.e from the right to left). Index -1 represents the last character of the string, -2 represents the second to last character and so on…

请注意,开始或结束索引可能为负数。 负索引意味着您从字符串的末尾开始而不是从头开始计数(即从右到左)。 索引-1代表字符串的最后一个字符,-2代表倒数第二个字符,依此类推……

  • Get the last character of the string

    获取字符串的最后一个字符

string = "freeCodeCamp"
print(string[-1])

Output:

输出:

> p
  • Get the last 5 characters of a string

    获取字符串的最后5个字符

string = "freeCodeCamp"
print(string[-5:])

Output:

输出:

> eCamp
  • Get a substring which contains all characters except the last 4 characters and the 1st character

    获取一个包含最后四个字符和第一个字符以外的所有字符的子字符串

string = "freeCodeCamp"
print(string[1:-4])

Output:

输出:

> reeCode

更多例子 (More examples)

str = “freeCodeCamp”print str[-5:-2] # prints ‘eCa’
print str[-1:-2] # prints ‘’ (empty string)
  • Get every other character from a string

    从字符串中获取所有其他字符

string = "freeCodeCamp"
print(string[::2])

Output:

输出:

> feCdCm

翻译自: https://www.freecodecamp.org/news/how-to-substring-a-string-in-python/

如何在Python中对字符串进行子字符串化相关推荐

  1. python中用什么函数读取字符串_如何在Python中获得函数名作为字符串?

    在Python中,如何在不调用函数的情况下以字符串的形式获得函数名? 1 2 3 4def my_function(): pass print get_function_name_as_string( ...

  2. 如何在Python中匹配最接近的字符串

    ■ 背景 手边有79个学校参加 智能车深度学习组别 提交的技术报告,但是在等级的EXCEL表格中对应的学校的名称与报告名称有些区别.那么如何能够比较自动对这些名称进行匹配呢? 这里使用了PYTHON库 ...

  3. python中的reg_如何在python中从注册表读取字符串格式的Reg_二进制类型值

    这些注册表项中似乎还有更多的数据,但它们以最近使用的程序文件的名称开头.我找到了一种提取名字的方法,但是条目以更多的数据结尾,我不知道它是什么.在 字符由空字节(b'\x00)分隔.所以我们必须每秒钟 ...

  4. python字符串筛选输出_如何在Python中过滤字符串列表

    Python使用列表数据类型在顺序索引中存储多个数据.它的工作方式类似于其他编程语言的数字数组.filter()方法是Python的一种非常有用的方法.可以使用filter()方法从Python中的任 ...

  5. 如何在Python中拆分字符串

    使用字符串时,常见的操作之一是使用给定的分隔符将字符串拆分为子字符串数组.在本文中,我们将讨论如何在Python中拆分字符串. PYthon学习企鹅裙:88198-2657 领取python自动化编程 ...

  6. python中glob模块怎么下_如何在Python中使用glob.glob模块搜索子文件夹?

    如何在Python中使用glob.glob模块搜索子文件夹? 我想在文件夹中打开一系列子文件夹,找到一些文本文件并打印一些文本文件行. 我用这个: configfiles = glob.glob('C ...

  7. python输入一串字符串、输出字符串中的数字_Python 字符串与数字输出方法 如何在python中输入字符串,输出十进制...

    python将字符串中的数字乘以2并输出整个字符串 2018 Amazon Jeff Bezos 1120 4036 Amazon Jeff Bezos 2240 def db(num): impor ...

  8. linux中python如何调用matlab的数据_特征锦囊:如何在Python中处理不平衡数据

    今日锦囊 特征锦囊:如何在Python中处理不平衡数据 ? Index 1.到底什么是不平衡数据 2.处理不平衡数据的理论方法 3.Python里有什么包可以处理不平衡样本 4.Python中具体如何 ...

  9. python中定义一个空的字符串_04python—15种字符串操作

    <python小白入门系列教程> 专栏 • 第04篇 字符串是 字符的序列 .字符串基本上就是一组单词.我几乎可以保证你在每个Python程序中都要用到字符串,所以请特别留心下面这部分的内 ...

最新文章

  1. 头条小视频和西瓜视频signature签名算法
  2. 计算机组成与系统结构+答案书,计算机组成与系统结构书本答案
  3. OpenGL键盘消息实例
  4. oracle 中序号的一种生成方式
  5. Tomcat监控xml ,当修改xml后,不用重启tomcat
  6. Java Keytool 命令行工具
  7. 人生苦短,我用python-- Day6 面向对象
  8. 对PostgreSQL中bgwriter的 MyProc 的理解
  9. js实现页面加载完毕之前显示Loading效果
  10. Oracle 函数进阶、分组排序、列转行、cast 类型转换、dbms_random 生成随机数、sys_guid
  11. SAP报表程序的下载上传
  12. 数学建模优化模型简单例题_数学建模案例分析--最优化方法建模7习题六
  13. 计算机主机结构讲解,电脑内部结构图和讲解
  14. goole搜索技巧(摘要)
  15. element中el-autocomplete实现中英文搜索功能
  16. 简单的实现app界面劫持和后台监控
  17. 代码报错 Uncaught TypeError: Converting circular structure to JSON
  18. 机器学习综述——机器学习理论基础与发展脉络
  19. java writeint_java的writeInt() 函数怎么用啊
  20. 用日记APP来记录生活,再也不用担心无法坚持写日记

热门文章

  1. python多线程并发_Python进阶记录之基础篇(二十四)
  2. 异步方法顺序调用问题
  3. 取消tableView多余的横线
  4. iOS中你可能没有完全弄清楚的(一)synthesize
  5. 判断数组里面的下标是否等于一个字符串
  6. 6 OC 中的isa 指针
  7. 利用jQuery实现用户名片小动画
  8. c# 元组Tuple
  9. Robotium初探秘
  10. 在数据库组件中用业务规则剖析挑选数据