Python中的切片是什麼?


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-09-15

2K+ 瀏覽次數

開啟您的 事業

完成該課程獲得認證

開始學習
廣告
© . All rights reserved.