如何在 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.