python字符串 切片_Python切片字符串

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:

输出:

HelloWorld
HelloWorld

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:

输出:

Hello
llo

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字符串切片函数创建子字符串的全部内容。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

翻译自: https://www.journaldev.com/23584/python-slice-string

python字符串 切片