如何在 Python 中使用切片操作符?


切片操作符用于切片字符串。slice() 函数也可以用于相同目的。我们将通过一些示例来了解切片操作符。

什么是切片?

Python 中的切片是从字符串中获取子字符串。切片范围作为参数设置,即开始、停止和步长。

语法

让我们看看语法。

# slicing from index start to index stop-1
arr[start:stop]

# slicing from index start to the end
arr[start:]

# slicing from the beginning to index stop - 1
arr[:stop]

# slicing from the index start to index stop, by skipping step
arr[start:stop:step]

让我们看看语法。

切片操作符示例

让我们看一个示例,其中我们将从开始到结束进行切片,并使用步长等。

myStr = 'Hello! How are you?' print("String = ",myStr) # Slice print("Slicing from index start to index stop-1 = ",myStr[5:8]) print("Slicing from index start to the end = ",myStr[3:]) print("Slicing from the beginning to index stop - 1 = ",myStr[:5]) print("Slicing from the index start to index stop, by skipping step = ",myStr[5:11:2])

输出

String =  Hello! How are you?
Slicing from index start to index stop-1 =  ! H
Slicing from index start to the end =  lo! How are you?
Slicing from the beginning to index stop - 1 =  Hello
Slicing from the index start to index stop, by skipping step =  !Hw

切片字符串示例

在这个示例中,我们将切片一个字符串。

# Create a String myStr = 'Hello! How are you?' # Display the String print("String = ",myStr) # Slice the string print("String after slicing = ",myStr[8:11])

输出

String =  Hello! How are you?
String after slicing =  ow 

带步长的字符串切片示例

步长用于设置切片每个索引之间的增量。

# Create a String myStr = 'Hello! How are you?' # Display the String print("String = ",myStr) # Slice the string with step print("String after slicing = ",myStr[8:15:2])

输出

String =  Hello! How are you?
String after slicing =  o r 

切片元组示例

我们可以切片元组的部分。

# Create a Tuple mytuple = ("Tim", "Mark", "Harry", "Anthony", "Forrest", "Alex", "Rock") # Display the Tuple print("Tuple = ",mytuple) # Slice the Tuple print("Tuple after slicing = ",mytuple[3:5])

输出

Tuple =  ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock')
Tuple after slicing =  ('Anthony', 'Forrest')

带步长的元组切片示例

我们可以切片元组的部分,并使用步长参数设置切片每个索引之间的增量。

# Create a Tuple mytuple = ("Tim", "Mark", "Harry", "Anthony", "Forrest", "Alex", "Rock", "Paul", "David", "Steve") # Display the Tuple print("Tuple = ",mytuple) # Slice the Tuple with step 2 print("Tuple after slicing = ",mytuple[2:8:2])

输出

Tuple =  ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock', 'Paul', 'David', 'Steve')
Tuple after slicing =  ('Harry', 'Forrest', 'Rock')

切片列表示例

# Create a List myList = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # Display the List print("List = ",myList) # Slice the List print("List after slicing = ",myList[3:6])

输出

List =  ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
List after slicing =  ['s', 't', 'u']

带步长的列表切片示例

我们可以切片列表的部分,并使用步长参数设置切片每个索引之间的增量。

# Create a List myList = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # Display the List print("List = ",myList) # Slice the List with step 2 print("List after slicing = ",myList[3:9:2])

输出

List =  ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
List after slicing =  ['s', 'u', 'w']

更新于: 2022年9月15日

3K+ 浏览量

启动你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.