博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python字符串 切片_Python切片字符串
阅读量:2529 次
发布时间:2019-05-11

本文共 2839 字,大约阅读时间需要 9 分钟。

python字符串 切片

Python string supports slicing to create substring. Note that Python string is immutable, slicing creates a new substring from the source string and original string remains unchanged.

Python字符串支持切片以创建子字符串。 请注意,Python字符串是不可变的,切片会根据源字符串创建一个新的子字符串,而原始字符串将保持不变。

Python切片字符串 (Python slice string)

Python slice string syntax is:

Python slice字符串语法为:

str_object[start_pos:end_pos:step]

The slicing starts with the start_pos index (included) and ends at end_pos index (excluded). The step parameter is used to specify the steps to take from start to end index.

切片以start_pos索引(包括)开始,以end_pos索引(排除)结束。 step参数用于指定从开始到结束索引执行的步骤。

Python String slicing always follows this rule: s[:i] + s[i:] == s for any index ‘i’.

Python字符串切片始终遵循以下规则: s [:i] + s [i:] == s用于任何索引'i'。

All these parameters are optional – start_pos default value is 0, the end_pos default value is the length of string and step default value is 1.

所有这些参数都是可选的– start_pos默认值为0,end_pos默认值为字符串的长度,step默认值为1。

Let’s look at some simple examples of string slice function to create substring.

让我们看一些创建子字符串的字符串切片函数的简单示例。

s = 'HelloWorld'print(s[:])print(s[::])

Output:

输出:

HelloWorldHelloWorld

Note that since none of the slicing parameters were provided, the substring is equal to the original string.

请注意,由于未提供任何切片参数,因此子字符串等于原始字符串。

Let’s look at some more examples of slicing a string.

让我们看一下切片字符串的更多示例。

s = 'HelloWorld'first_five_chars = s[:5]print(first_five_chars)third_to_fifth_chars = s[2:5]print(third_to_fifth_chars)

Output:

输出:

Hellollo

Note that index value starts from 0, so start_pos 2 refers to the third character in the string.

请注意,索引值从0开始,因此start_pos 2引用字符串中的第三个字符。

使用切片反转字符串 (Reverse a String using Slicing)

We can reverse a string using slicing by providing the step value as -1.

通过将步进值设置为-1,我们可以使用切片来反转字符串。

s = 'HelloWorld'reverse_str = s[::-1]print(reverse_str)

Output: dlroWolleH

输出: dlroWolleH

Let’s look at some other examples of using steps and negative index values.

让我们看看其他使用步骤和负索引值的示例。

s1 = s[2:8:2]print(s1)

Output: loo

输出: loo

Here the substring contains characters from indexes 2,4 and 6.

这里的子字符串包含来自索引2,4和6的字符。

s1 = s[8:1:-1]print(s1)

Output: lroWoll

输出: lroWoll

Here the index values are taken from end to start. The substring is made from indexes 1 to 7 from end to start.

这里的索引值是从头到尾的。 子字符串从头到尾由索引1到7组成。

s1 = s[8:1:-2]print(s1)

Output: lool

输出: lool

Python slice works with negative indexes too, in that case, the start_pos is excluded and end_pos is included in the substring.

Python slice也可以使用负索引,在这种情况下,排除start_pos并将end_pos包括在子字符串中。

s1 = s[-4:-2]print(s1)

Output: or

输出: or

Python string slicing handles out of range indexes gracefully.

Python字符串切片可正常处理超出范围的索引。

>>>s = 'Python'>>>s[100:]''>>>s[2:50]'thon'

That’s all for python string slice function to create substring.

这就是python字符串切片函数创建子字符串的全部内容。

. 检出完整的python脚本和更多Python示例。

翻译自:

python字符串 切片

转载地址:http://vamzd.baihongyu.com/

你可能感兴趣的文章
ssh REMOTE HOST IDENTIFICATION HAS CHANGED!
查看>>
PHP中$_SERVER的详细参数与说明
查看>>
好用的方法
查看>>
JavaScript实验一(添加节点,删除节点)
查看>>
vue的基础知识
查看>>
硬件工程师必会电路模块之三极管实用电路(转)
查看>>
HDU2602 01背包入门 Bone Collector【01背包模板题】
查看>>
总结一下eclipse中如何使用断言
查看>>
win7下IIS的安装和配置 图文教程
查看>>
redis 命令
查看>>
linux中likely()和unlikely()
查看>>
shell 截取指定的字符串
查看>>
参考文档链接地址-个人比较推荐的
查看>>
《教父》曾说
查看>>
你懂得C,所以C++也不在话下
查看>>
4 cdh 5.12 centos 6.10三节点安装
查看>>
5 centos 6.10 三节点安装apache hadoop 2.9.1
查看>>
POJ 3728 The merchant(LCA+DP)
查看>>
三步学会Java Socket编程(三)
查看>>
unix网络编程第三版源代码ubuntu下配置的问题解决
查看>>